2015-01-03 242 views
-1

我有一個表格,我希望能夠選中標題中的一個複選框以選擇該表格中的所有複選框。這通常應該是一個非常簡單的事情,但它根本不在這裏工作。當我在JSfiddle上運行示例代碼時,它可以工作。當在我的開發環境中運行它並檢查頂部框時,如果我把alert()它會提醒,但仍然不檢查其餘的複選框。任何想法爲什麼?選中一個複選框以選擇多個複選框

[JS fiddle][1] 

http://jsfiddle.net/pkvLqe1d/ 

這裏是我的開發環境中的代碼:

<div class="content" id="content"> 
<form id="selectContent" method="post" class="grow"> 
<table class="static" style="width:1000px;"> 
    <thead> 
    <tr> 
     <th> 
     <b>Id</b> 
     </th> 
     <th> 
     <b>Name</b> 
     </th> 
     <th> 
     <label><input type="checkbox" name="select-all" id="select-all" /></label> 
     </th> 
    </tr> 
    </thead> 
    <tbody> 
    <?php 
    foreach($a as $b) 
    { 
     echo "<tr>";  
     echo "<td>$b->id</td>"; 
     echo "<td>$b->name</td>"; 
     echo "<td><label><input type='checkbox' name='id[]' value='".trim($b->id)."' class='round'></label></td>"; 
     echo "</tr>"; 

    } 
    ?> 

    </tbody> 
</table> 
</form> 


</div> 

<script> 
//Listen for click on toggle checkbox 
$('#select-all').click(function(event) { 
    if(this.checked) { 

     // Iterate each checkbox 
     $(':checkbox').each(function() { 

     this.checked = true; 

     }); 
    } 
}); 
</script> 
+3

好吧,當我運行你的小提琴,它的工作原理,就像你說的。這意味着還有其他問題,並且您沒有向我們展示您的真實代碼。這將很難幫助你。 (SO的一個要求是說明問題的「最小工作示例」,我們可以請看看嗎?) –

+0

這裏是一個JSfiddle,它不工作:http://jsfiddle.net/04btkoL5/ – mediasurface

+0

當我在Firebug調試器中運行它,它在參數列表後顯示「SyntaxError:missing)」。 (由於JSFiddle的工作方式,行號並沒有用。)當用調試器運行它時會發生什麼? –

回答

0

由於我使用的ezmark複選框,該解決方案是在腳本的末尾,如下圖所示添加.change()。這將只需設置檢索複選框等於我們最近修改的「主」複選框的選中狀態:

$('#select-all').click(function() { 
$(':checkbox').prop('checked',this.checked).change(); 
}); 
相關問題