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

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

programmeradmin0浏览0评论

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");
 });
发布评论

评论列表(0)

  1. 暂无评论