开发者

Why Jquery each function stopped working?

This code of mine was working well, but now it has stopped working!! What might be wrong with it? Am looping through table rows, using the JQuery .each function,

html:

 <form method="POST" action="/post_updates/" onSubmit="return postUpdates()" >
    <table id="mytable" class="table">
      <thead>
          <tr>
             <th>col 1</th>
             <th>col2 </th>
          </tr>
       </thead>
       <tbody>
       !-- rows are created dynamically --
       {% for object in object_list %}
         <tr>
          <td id="row">{{object.id}}</td>
          !-- other td's --
         </tr>
        {% endfor %}
    </tbody>
   </table>

Javascript:

<script type="text/javascript" >
  function postUpdates(){
    $("#mytable tr:gt(0)").each( function () {
       // this code NEVER get executed
      var rowid = $(this).fin开发者_如何学JAVAd("#row").html();
      // .. does alot of stuff with rowid!!
    });
  }

am sure this was working, but it just stopped. Tested it in both Chrome and Firefox!

Gath.


you missed some parenthesis:

$("#mytable tr:gt(0)").each( function() {
   // this code NEVER get executed
  var rowid = $(this).find("row").html();
});

also your selector is missing something... I am assuming by your issue that the problem is before that, but .find("row") would not generally find something.


Missing parens:

$("#mytable tr:gt(0)").each( function() {
                                     ^^

Update: You are using the same id with multiple elements, which is not allowed. Change id="row" to class="row" and use the selector .find(".row") instead.


Below is how your markup should look after the scripting engine renders it. Notice the change in the onSubmit function and also the declaration of the function before it is invoked. As you are using jquery you should probably consider using their event binding api. In this case you should use submit event

<script type="text/javascript">
    function postUpdates(){
    $("#mytable tr:gt(0)").each( function () {
       // this code NEVER get executed
      alert( $(this).find("#row").html());
      // .. does alot of stuff with rowid!!
    });
  }
</script>
<form method="POST" action="" onSubmit="postUpdates()" >
    <table id="mytable" class="table">
      <thead>
          <tr>
             <th>col 1</th>
             <th>col2 </th>
          </tr>
       </thead>
       <tbody>


         <tr>
          <td id="row">test</td>
             <td>test2</td>
         </tr>

    </tbody>
   </table>
    <input type="submit" />
</form>

Hope this helps.

Demo

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜