javascript - jquery .on() only works once - Stack Overflow

I have a table with multiple checkbox inputs:<form><table id="table"><tr>&

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?

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?

Share Improve this question asked Aug 21, 2013 at 11:29 user2653629user2653629 751 silver badge4 bronze badges 4
  • You might want to try hooking on an element that is not going to be reload (e.g. <form> in your example) with an appropriate filter so it only handles the events from the checkbox. – billc.cn Commented Aug 21, 2013 at 11:32
  • the .on should be executed on a parent element that doesnt change or document. – Patrick Evans Commented Aug 21, 2013 at 11:33
  • You're code seems fine. Check this out. jsfiddle/LEukL Look at your console and see if maybe something is crashing your code and that's why it stops executing. – Daniel Wardin Commented Aug 21, 2013 at 11:34
  • For some context to the other ments or answers, .on() has documentation about delegated events which you're supposed to use here. – millimoose Commented Aug 21, 2013 at 11:35
Add a ment  | 

2 Answers 2

Reset to default 6

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");
 });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745041001a4607805.html

相关推荐

  • javascript - jquery .on() only works once - Stack Overflow

    I have a table with multiple checkbox inputs:<form><table id="table"><tr>&

    8小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信