最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Checkbox click event firing twice - Stack Overflow

programmeradmin2浏览0评论

I am using jQuery DataTables inside a modal. From that table I have a column in which it contains checkboxes. The first attempt in getting values of the checked checkboxes is all ok. However, when I close the modal and choose again, the checkbox click event is firing twice. Here is my code:

//handle event for checkbox checking.
arresting_officers_table.on("change", ":checkbox", function() { 
  if($(this).is(':checked')){
    console.log('Adding!'); //this log fires twice when clicking only 1 checkbox
    //alert('checked! ' + $(this).attr('data-value'));
    arresting_officers_ids.push($(this).attr('data-value'));
    arresting_officers_names.push($(this).attr('data-name'));
  } else {
    //remove item
    var idx = arresting_officers_ids.indexOf($(this).attr('data-value'));
    arresting_officers_ids.splice(idx, 1);
    arresting_officers_names.splice(idx, 1);
  }
});

Your response will be greatly appreciated. Thanks!

I am using jQuery DataTables inside a modal. From that table I have a column in which it contains checkboxes. The first attempt in getting values of the checked checkboxes is all ok. However, when I close the modal and choose again, the checkbox click event is firing twice. Here is my code:

//handle event for checkbox checking.
arresting_officers_table.on("change", ":checkbox", function() { 
  if($(this).is(':checked')){
    console.log('Adding!'); //this log fires twice when clicking only 1 checkbox
    //alert('checked! ' + $(this).attr('data-value'));
    arresting_officers_ids.push($(this).attr('data-value'));
    arresting_officers_names.push($(this).attr('data-name'));
  } else {
    //remove item
    var idx = arresting_officers_ids.indexOf($(this).attr('data-value'));
    arresting_officers_ids.splice(idx, 1);
    arresting_officers_names.splice(idx, 1);
  }
});

Your response will be greatly appreciated. Thanks!

Share Improve this question edited Nov 5, 2017 at 16:01 Shiladitya 12.2k17 gold badges28 silver badges42 bronze badges asked Apr 18, 2015 at 5:00 iamjc015iamjc015 2,2456 gold badges33 silver badges62 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

use below code. add .off("change") before attach event.

read more about .off()

Remove an event handler.

I assume you attach change event every time when model box is open.

arresting_officers_table.off("change").on("change", ":checkbox", function() { 
  if($(this).is(':checked')){
    console.log('Adding!'); //this log fires twice when clicking only 1 checkbox
    //alert('checked! ' + $(this).attr('data-value'));
    arresting_officers_ids.push($(this).attr('data-value'));
    arresting_officers_names.push($(this).attr('data-name'));
  } else {
    //remove item
    var idx = arresting_officers_ids.indexOf($(this).attr('data-value'));
    arresting_officers_ids.splice(idx, 1);
    arresting_officers_names.splice(idx, 1);
  }
});

other option is attach event every time you can use Event Delegation to attach event to dynamic generated element . you can read more about Event Delegation

Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.

发布评论

评论列表(0)

  1. 暂无评论