谁能告诉我如何使用 JavaScript 选择从数据库获取的所有项目到复选框中


can anyone tell me how to use javascript for selecting all items fetched from database into a checkbox

这是从数据库中获取数据并填充复选框的代码:

<td>Applicable State</td>
<td><input type="checkbox" id="select2">Select All State</td>
<?php
$conn=get_dbconnect();
$sql="select name from states";
$rs = mysqli_query($conn, $sql);//odbc_exec($conn,$sql);
if(mysqli_num_rows($rs) > 0) {
    while($row = mysqli_fetch_assoc($rs)) {
        echo "<input type='checkbox' value='".$row['name']."' onclick='check(this)'; /> ".$row['name'];  
    }
}
mysqli_close($conn); [enter image description here][1]
?> 

你的答案在这里,伙计:

网页部分

<form action="#">
<p><label><input type="checkbox" id="checkAll"/> Check all</label></p>
<fieldset>
    <legend>Loads of checkboxes</legend>
    <p><label><input type="checkbox" /> Option 1</label></p>
    <p><label><input type="checkbox" /> Option 2</label></p>
    <p><label><input type="checkbox" /> Option 3</label></p>
    <p><label><input type="checkbox" /> Option 4</label></p>
</fieldset>

Jquery 部分

$("#checkAll").change(function () {
    $("input:checkbox").prop('checked', $(this).prop("checked"));
});

重要提示 不要忘记在标头标记中包含 jquery CDN

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>