2016-07-13 46 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;       
     }); 
     } 
     }); 
相关问题