简体   繁体   中英

jquery .on() only works once

I have a table with multiple checkbox inputs:

<form>
<table id="table">
    <tr>
      <td><input type="checkbox" value="1" class="someClass"></td>
      <td><input type="checkbox" value="1" class="someClass"></td>...

And some jquery that refreshes the table from another file when the box is checked/unchecked:

$(".someClass").on("change", function() {
        $('#edit').ajaxSubmit(); //Submitting the form with id "edit"
        $("#table").load("tablerefresh");
 });

My problem is that when I check/uncheck a box, the table will refresh only once, and it should do it every time I check/uncheck the box, I've looked everywhere and can't seem to find a solution. Any ideas?

This is probably a matter of delegation, since #table doesn't change, use it as the scope, targeting .someClass inside:

$("#table").on("change", ".someClass", function() {
  $('#edit').ajaxSubmit(); //Submitting the form with id "edit"
  $("#table").load("tablerefresh");
});

Note:

You can also use delegate() :

$("#table").delegate(".someClass", "change", function(){
  //Code
});

try this:

$(document).on("change", ".someClass" , function() {
        $('#edit').ajaxSubmit(); //Submitting the form with id "edit"
        $("#table").load("tablerefresh");
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM