我编写了这样的代码来选择所提取数据中的所有复选框
<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 } ?>
我写过这样的java脚本
$('#select-all').click(function(event) {
if(this.checked) {
// Iterate each checkbox
$('#sendmsg[]').each(function() {
this.checked = true;
});
}
});
如何编写corect脚本以选中所有复选框
答案 0 :(得分: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" >
试试这个
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 } ?>
答案 1 :(得分:0)
按名称获取元素,如果您使用#,则按ID
使用其元素$('#select-all').click(function(event) {
if(this.checked) {
// Iterate each checkbox
$('[name="sendmsg[]"]').each(function() {
this.checked = true;
});
}
});