2016-07-13 47 views
-2

我寫了這樣的代碼在所獲取的數據來選擇所有複選框如何檢查標記所有checkbocx​​es通過選擇一個複選框

<input type="checkbox" name="select-all" id="select-all" /> 
    <?php 
    while ($row = mysql_fetch_array($users)) { 
?> 
<tr> 
<td><span><?php echo $row["id"] ?></span></td> 
<td><span><?php echo $row["emailid"] ?></span></td> 
<td><span class="wrapper"><input type="checkbox" name="sendmsg[]" value="<?php echo $row["emailid"] ?>"/></span></td> 
</tr> 
<?php } ?> 

我寫了這樣

$('#select-all').click(function(event) { 
    if(this.checked) { 
    // Iterate each checkbox 
    $('#sendmsg[]').each(function() { 
     this.checked = true;       
     }); 
     } 
     }); 

Java腳本如何編寫corect腳本以獲取所有選中的複選框

+0

你的HTML是不是因爲它是有效的,你不能有輸入ele和兄弟姐妹一樣。 – Esko

+0

我已添加新代碼plezse試試這種方式 –

回答

0

function toggle(source) { 
 
    checkboxes = document.getElementsByName('checkbox[]'); 
 
    if (x = 1) { 
 
     for (var i = 0, n = checkboxes.length; i < n; i++) { 
 
      checkboxes[i].checked = source.checked; 
 
     } 
 
    } 
 
}
<input type="checkbox" onClick="toggle(this)" /> 
 
<input name="checkbox[]" type="checkbox" > 
 
<input name="checkbox[]" type="checkbox" > 
 
<input name="checkbox[]" type="checkbox" >

如果您正在使用#那麼它的元素通過ID其名稱試試這個

function toggle(source) { 
 
    checkboxes = document.getElementsByName('sendmsg[]'); 
 
    
 
     for (var i = 0, n = checkboxes.length; i < n; i++)  { 
 
      checkboxes[i].checked = source.checked; 
 
     
 
     } 
 
}
<input type="checkbox" name="select-all" id="select-all" onClick="toggle(this)" /> 
 
     <?php 
 
     while ($row = mysql_fetch_array($users)) { 
 
    ?> 
 
    <tr> 
 
    <td><span><?php echo $row["id"] ?></span></td> 
 
    <td><span><?php echo $row["emailid"] ?></span></td> 
 
    <td><span class="wrapper"><input type="checkbox" name="sendmsg[]" value="<?php echo $row["emailid"]; ?></span></td> 
 
    </tr> 
 
    <?php } ?>

+0

什麼是'x' ....? –

0

GET元素

$('#select-all').click(function(event) { 
    if(this.checked) { 
    // Iterate each checkbox 
    $('[name="sendmsg[]"]').each(function() { 
     this.checked = true;       
     }); 
     } 
     }); 
相關問題