php
// iterating through the data and displaying in table format
foreach($data['schoolList'] as $school)
{
echo'<tr>';
echo '<td><input type=checkbox class=check id=check value='.$school['sid'].'></td>';
echo '<td>'.$school['sid'] .'</td>';
echo '<td>'.$school['name'] .'</td>';
echo '<td>'.$school['specialization'].'</td>';
echo'</tr>';
}
echo '<button onclick="myFunction()">Apply</button>';
JavaScript
<script>
//to restrict the user to select only 3 checkboxes
$("input:checkbox").click(function() {
let x = $("input:checkbox:checked").length >= 3;
$("input:checkbox").not(":checked").attr("disabled",x);
});
let y=[];
function myFunction() {
y= document.getElementById("check").value;
console.log(y);
}
</script>
即使我选择了多个复选框,无论选择什么,它都只会记录第一个学校ID!谁能帮我记录所有控制台值!谢谢!
答案 0 :(得分:1)
document.getElementById("check")
仅返回id="check"
中的第一个元素,因为ID是唯一标识符。
改为使用getElementsByClassName()
或使用jQuery:
<script>
//to restrict the user to select only 3 checkboxes
$("input:checkbox").click(function() {
let x = $("input:checkbox:checked").length >= 3;
$("input:checkbox").not(":checked").prop("disabled", x);
});
let y = [];
function myFunction() {
$(":checked").each(function () {
y.push($(this).val());
});
console.log(y);
}
</script>