3 回答
TA贡献1893条经验 获得超10个赞
这是检查所有物品并收集物品的方法
function sumIt(checkboxes) {
var values = [];
checkboxes.forEach(function(box) {
if (box.checked) values.push(box.value)
})
document.getElementById("studID").value = values.join(",");
}
window.addEventListener("load", function() {
var checkboxes = document.getElementsByName("user_id[]");
document.getElementById("checkUncheckAll").addEventListener("click", function() {
var checked = this.checked;
checkboxes.forEach(function(box) {
box.checked = checked;
})
sumIt(checkboxes)
})
checkboxes.forEach(function(box) {
box.addEventListener("click", function() {
sumIt(checkboxes)
})
})
})
<table>
<tr>
<th colspan="3"><input type="checkbox" id="checkUncheckAll" />Check all</th>
</tr>
<tr>
<td><input type="checkbox" name="user_id[]" value='1' /></td>
<td><input type="checkbox" name="user_id[]" value='2' /></td>
<td><input type="checkbox" name="user_id[]" value='3' /></td>
</tr>
</table>
<input class="form-control" name="user_id" id="studID" type="text" maxlength="255" />
- 3 回答
- 0 关注
- 152 浏览
添加回答
举报