te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to know which button is clicked - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to know which button is clicked - Stack Overflow

programmeradmin2浏览0评论

In my page I have many edit buttons each name starts with "edit" and then some id. I want to know any of my edit buttons is clicked or not. In details, I have form. In form I have many edit buttons which name starts with "edit" , delete buttons which name starts with "delete" and 1 add button. All are submit buttons. form onsubmit I call JavaScript function in which I want if the button is edit confirm("some text") else submit form. How can I do that in JavaScript? I think give all these buttons same id and then getElementById but then how con I change?

In my page I have many edit buttons each name starts with "edit" and then some id. I want to know any of my edit buttons is clicked or not. In details, I have form. In form I have many edit buttons which name starts with "edit" , delete buttons which name starts with "delete" and 1 add button. All are submit buttons. form onsubmit I call JavaScript function in which I want if the button is edit confirm("some text") else submit form. How can I do that in JavaScript? I think give all these buttons same id and then getElementById but then how con I change?

Share Improve this question edited Jan 13, 2014 at 12:27 BenMorel 36.6k51 gold badges205 silver badges335 bronze badges asked May 5, 2011 at 10:44 Aram GevorgyanAram Gevorgyan 2,2058 gold badges39 silver badges57 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 5

This is simple using jQuery:

$(':button').click(function() {
  // reference clicked button via: $(this)
  var buttonElementId = $(this).attr('id');
});

Try it out: http://jsfiddle/7YEay/

UPDATE based on feedback in ments

This is untested/pseudo code:

$(':submit').click(function(event) {
  var buttonName = $(this).attr('name');

  if (buttonName.indexOf('edit') >= 0) {
    //confirm("some text") logic...
  }

  event.preventDefault();
});

This documentation may be helpful too: http://api.jquery./submit/

function MyOnSubmit(e){
   e = e || window.event;
   // srcElement for IE, target for w3c
   var target = e.target || e.srcElement;
   if (target.id.indexOf("edit") > -1){
     // an edit button fired the submit event
   }
}

although i advice you research further to find a better way to handle edit and delete buttons (like making them links with href=editpage.jsp?id=23)

I'm pretty sure you just answered this yourself...

Each starts with "edit", and ends with a unique ID? So... $(button).attr("id") would give you that. Store it in a variable? Not sure what you're trying to do..

bind click event on all button:

for (var i = 0; i < buttons.length; i++) {
    var button = buttons[i];
    if (button.addEventListener) {
        button.addEventListener('click', handler, false);
    }
    else {
        button.attachEvent('onclick', handler);
    }
}

in your event handler, get the Event object, and then get the target:

function handler(e) {
    e = e || window.event;
    // srcElement for IE, target for w3c
    var target = e.target || e.srcElement;
    var id = target.name.substring(4);
    /* your code rely on id */
}

I think you might not have phrased your question correctly. If you are using jquery, locating the button is as easy as $('#id'), and if you want to store any information on that button, you can either add an attribute or use jquery.data function.

$('#id').attr("pressed", "true");

Or

$('#id').data('pressed', 'true');
发布评论

评论列表(0)

  1. 暂无评论