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 關注
- 171 瀏覽
添加回答
舉報