Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 710
user2653629 Avatar asked Mar 23 '23 17:03

user2653629


1 Answers

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
});
like image 166
George Avatar answered Apr 02 '23 14:04

George