Re: [問題] jquery 抓取值 的問題

看板Ajax作者 (沉默是金)時間16年前 (2008/12/22 22:34), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《drinks (爪哇工程師)》之銘言: : <form > : <table id="table1"> : <tr> : <td><input type="checkbox" class="all"></td> : <td>數字</td> : </tr> : <tr class="addtr"> : <td><input type="checkbox" class="chk"></td> : <td class="add">123</td> : </tr> : <tr class="addtr"> : <td><input type="checkbox" class="chk"></td> : <td class="add">456</td> : </tr> : </table> : </form> : 以下是 jquery 程式碼 : <script type="text/javascript"> : $(document).ready(function(){ : $('#clickAll').click(function(){//全選 : if (this.checked){ : $("#table1 input:checkbox.chk").attr('checked','checked'); : }else{ : $("#table1 input:checkbox.chk").attr('checked',''); : } : }); : //第二個 我是想要在 checkbox有點選的時候 將td的數字相加 : //我寫的方法 抓不到check box .checked : $("#table1 input:checkbox.chk").click(function () { : var total = 0; : $('#table1 tr.addtr') .filter(function(){ : return this; : }) ↑這個filter沒有任何意義吧? .each( function(){ : alert($(this).children(".chk").checked ); ^^^^^^^這個This是 tr , tr 的children 是指他底下的那一層 td , 所以你撈不到 checkbox. 你應該用find會比較理想,find會深入查找. $(this).find(".chk"); : //這邊 如果可以抓到.checked ,那就可以用if else把數字累加 : //可是 alert的值 都是undefinded : alert($(this).children(".add").text() ); : //這邊可以抓到正確的值 : }); : }); : }); : </script> : 不好意思 寫的有點亂, : 請問有人知道我錯在哪裡嗎? : 感謝回答! -- What do you want to have ? / What do you have? 從書本中,你可以發現我的各種興趣。 從CD中,你可以瞭解我所喜歡的偶像明星。 或許從文字你很難以瞭解一個人,但從物品可以。 My PPolis , My past. http://ppolis.tw/user/Tony -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 221.169.78.140

12/23 12:55, , 1F
感謝回答!
12/23 12:55, 1F
文章代碼(AID): #19JwLUrr (Ajax)
文章代碼(AID): #19JwLUrr (Ajax)