Re: [問題] jquery 抓取值 的問題
※ 引述《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
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章