如何循环遍历 HTML Table 中的复选框行 Javascript
How to Loop Through Rows of Checkboxes in an HTML Table in Javascript
我有一个关于如何在 table 中循环检查复选框的问题。
我想要做的是在 table 中每行创建两个复选框以及一个下拉菜单。如果每行的两个复选框都被选中,我将 运行 javascript 中的条件。如果连续选中两个复选框以及下拉菜单选项,我将 运行 一个条件。
所以我想遍历 table 的每一行,检查是否选中了两个复选框。我认为另一种方法是使用 div 和 类。我该怎么做?
我下面的示例代码不在 table 中,但这是我目前所拥有的,只是为了让复选框和下拉菜单正常工作。
<html>
<div class="container">
<input type="checkbox" class="checks" value ="Apple">Apple<br>
<input type="checkbox" class="checks" value ="BananaValue">Banana<br>
<input type="checkbox" class="checks" value ="Carrot">Carrot<br>
<form>
Select your favorite fruit:
<select id="mySelect" /*onchange="run();"*/>
<option value="apple">apple</option>
<option value="orange">Orange</option>
<option value="pineapple">Pineapple</option>
<option value="banana">Banana</option>
</select>
</form>
<input type="submit" onclick="run()" />
<script>
function run() {
var checks = document.getElementsByClassName('checks');
var str = '';
for (i = 0; i < 3; i++) {
if (checks[i].checked === true) {
str += checks[i].value + " ";
}
}
alert(str);
}
</script>
</div>
</html>
更新:
代码已更新以使用您评论中的信息。单击一个按钮时只会运行
原稿:
我注释掉了(但保留了)代码以备日后需要。这是您想要的(我认为)的一个工作示例。在 window.load 侦听器中,我们首先遍历所有行以查看是否有任何行准备好 运行 条件。然后我们在 table 中的每个表单元素上设置 change
侦听器。侦听器侦听更改事件,然后 运行 我们的行检查。
/*
window.addEventListener('load', () => {
document.querySelectorAll('.theTable tr').forEach(row => checkRow(row))
document.querySelectorAll('.theTable td input[type=checkbox], .theTable td select').forEach(input => {
input.addEventListener('change', e => checkRow(e.target.closest('tr')))
})
})
*/
function run() {
document.querySelectorAll('.theTable tr').forEach((row, i) => {
let allChecked = row.querySelectorAll('input[type=checkbox]').length === row.querySelectorAll('input[type=checkbox]:checked').length
if (allChecked && row.querySelectorAll('select option:checked').length > 0 && row.querySelector('select option:checked').value !== '') {
console.log('run conditional on row index ', i)
}
})
}
<input type="submit" onclick="run()" />
<table class='theTable'>
<tr>
<form>
<td>
<label> <input type="checkbox" class="checks" value="Apple" checked>Apple </label>
<label> <input type="checkbox" class="checks" value="BananaValue" checked>Banana </label>
<label> <input type="checkbox" class="checks" value="Carrot" checked>Carrot </label>
</td>
<td>
Select your favorite fruit:
<select id="mySelect">
<option value=""></option>
<option value="apple">apple</option>
<option value="orange">Orange</option>
<option selected value="pineapple">Pineapple</option>
<option value="banana">Banana</option>
</select>
</td>
</form>
</tr>
<tr>
<form>
<td>
<label> <input type="checkbox" class="checks" value="Apple">Apple </label>
<label> <input type="checkbox" class="checks" value="BananaValue">Banana </label>
<label> <input type="checkbox" class="checks" value="Carrot">Carrot </label>
</td>
<td>
Select your favorite fruit:
<select id="mySelect">
<option value=""></option>
<option value="apple">apple</option>
<option value="orange">Orange</option>
<option value="pineapple">Pineapple</option>
<option value="banana">Banana</option>
</select>
</td>
</form>
</tr>
</table>
我对问题的初始条件的理解有点不同,
因此这可能不足以作为可接受的答案。
- 我读到每行只允许选择 2 种蔬菜。
- 仅当满足 #1 条件时才添加 select 水果选择。
考虑到这一点,这是我提交的解决方案。
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0, user-scalable=yes"/>
<!-- From: -->
<title> Table Food Choices </title>
<!-- link rel="stylesheet" href="common.css" media="screen" -->
<style>
</style>
<body>
<form>
<h3> From each row: </h3>
<table id="tbl">
<thead>
<tr>
<th colspan="3">Select 2 vetables</th>
<th>And your favorite fruit:</th>
</tr>
</thead>
<tbody>
<tr>
<td> <label><input type="checkbox" class="checks" value ="Beans">Green Bean</label></td>
<td> <label><input type="checkbox" class="checks" value ="Corn">Corn</label></td>
<td> <label><input type="checkbox" class="checks" value ="Carrot">Carrot</label></td>
<th>
<select>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="grape">Grape</option>
<option value="orange">Orange</option>
<option value="peach">Peach</option>
<option value="pineapple">Pineapple</option>
<option value="tangerine">Tangerine</option>
</select>
</th>
</tr>
<tr>
<td> <label><input type="checkbox" class="checks" value ="Beans">Green Bean</label></td>
<td> <label><input type="checkbox" class="checks" value ="Corn">Corn</label></td>
<td> <label><input type="checkbox" class="checks" value ="Carrot">Carrot</label></td>
<th>
<select>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="grape">Grape</option>
<option value="orange">Orange</option>
<option value="peach">Peach</option>
<option value="pineapple">Pineapple</option>
<option value="tangerine">Tangerine</option>
</select>
</th>
</tr>
<tr>
<td> <label><input type="checkbox" class="checks" value ="Beans">Green Bean</label></td>
<td> <label><input type="checkbox" class="checks" value ="Corn">Corn</label></td>
<td> <label><input type="checkbox" class="checks" value ="Carrot">Carrot</label></td>
<th>
<select>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="grape">Grape</option>
<option value="orange">Orange</option>
<option value="peach">Peach</option>
<option value="pineapple">Pineapple</option>
<option value="tangerine">Tangerine</option>
</select>
</th>
</tr>
</tbody>
</table>
</form>
<input id="picks" type="submit" value="Submit choices">
<br>
Choices
<pre id='choices'></pre>
<script>
function run(){
var
tblRows = document.querySelectorAll('tbody tr'),
checks = document.querySelectorAll('.checks'),
selects = document.querySelectorAll('select'),
strs = []; // console.log(tblRows.length, checks.length, selects.length);
for (r = 0; r < tblRows.length; r++) {
let cbCount = 0, tstr = '';
for (cb=0; cb < 3; cb++) {
if ( checks[(r*3)+cb].checked ) {
cbCount++;
tstr += checks[(r*3)+cb].value + ' ';
}
}
if ( cbCount == 2 ) { strs.push(tstr + " " + selects[r].value); } // accept only 2 checkbox with 1 select values
}
document.getElementById('choices').textContent = strs.join('\n');
}
function init() {
document.getElementById('picks').addEventListener('click', run);
}
init();
</script>
</body>
</html>
我有一个关于如何在 table 中循环检查复选框的问题。
我想要做的是在 table 中每行创建两个复选框以及一个下拉菜单。如果每行的两个复选框都被选中,我将 运行 javascript 中的条件。如果连续选中两个复选框以及下拉菜单选项,我将 运行 一个条件。
所以我想遍历 table 的每一行,检查是否选中了两个复选框。我认为另一种方法是使用 div 和 类。我该怎么做?
我下面的示例代码不在 table 中,但这是我目前所拥有的,只是为了让复选框和下拉菜单正常工作。
<html>
<div class="container">
<input type="checkbox" class="checks" value ="Apple">Apple<br>
<input type="checkbox" class="checks" value ="BananaValue">Banana<br>
<input type="checkbox" class="checks" value ="Carrot">Carrot<br>
<form>
Select your favorite fruit:
<select id="mySelect" /*onchange="run();"*/>
<option value="apple">apple</option>
<option value="orange">Orange</option>
<option value="pineapple">Pineapple</option>
<option value="banana">Banana</option>
</select>
</form>
<input type="submit" onclick="run()" />
<script>
function run() {
var checks = document.getElementsByClassName('checks');
var str = '';
for (i = 0; i < 3; i++) {
if (checks[i].checked === true) {
str += checks[i].value + " ";
}
}
alert(str);
}
</script>
</div>
</html>
更新: 代码已更新以使用您评论中的信息。单击一个按钮时只会运行
原稿:
我注释掉了(但保留了)代码以备日后需要。这是您想要的(我认为)的一个工作示例。在 window.load 侦听器中,我们首先遍历所有行以查看是否有任何行准备好 运行 条件。然后我们在 table 中的每个表单元素上设置 change
侦听器。侦听器侦听更改事件,然后 运行 我们的行检查。
/*
window.addEventListener('load', () => {
document.querySelectorAll('.theTable tr').forEach(row => checkRow(row))
document.querySelectorAll('.theTable td input[type=checkbox], .theTable td select').forEach(input => {
input.addEventListener('change', e => checkRow(e.target.closest('tr')))
})
})
*/
function run() {
document.querySelectorAll('.theTable tr').forEach((row, i) => {
let allChecked = row.querySelectorAll('input[type=checkbox]').length === row.querySelectorAll('input[type=checkbox]:checked').length
if (allChecked && row.querySelectorAll('select option:checked').length > 0 && row.querySelector('select option:checked').value !== '') {
console.log('run conditional on row index ', i)
}
})
}
<input type="submit" onclick="run()" />
<table class='theTable'>
<tr>
<form>
<td>
<label> <input type="checkbox" class="checks" value="Apple" checked>Apple </label>
<label> <input type="checkbox" class="checks" value="BananaValue" checked>Banana </label>
<label> <input type="checkbox" class="checks" value="Carrot" checked>Carrot </label>
</td>
<td>
Select your favorite fruit:
<select id="mySelect">
<option value=""></option>
<option value="apple">apple</option>
<option value="orange">Orange</option>
<option selected value="pineapple">Pineapple</option>
<option value="banana">Banana</option>
</select>
</td>
</form>
</tr>
<tr>
<form>
<td>
<label> <input type="checkbox" class="checks" value="Apple">Apple </label>
<label> <input type="checkbox" class="checks" value="BananaValue">Banana </label>
<label> <input type="checkbox" class="checks" value="Carrot">Carrot </label>
</td>
<td>
Select your favorite fruit:
<select id="mySelect">
<option value=""></option>
<option value="apple">apple</option>
<option value="orange">Orange</option>
<option value="pineapple">Pineapple</option>
<option value="banana">Banana</option>
</select>
</td>
</form>
</tr>
</table>
我对问题的初始条件的理解有点不同, 因此这可能不足以作为可接受的答案。
- 我读到每行只允许选择 2 种蔬菜。
- 仅当满足 #1 条件时才添加 select 水果选择。
考虑到这一点,这是我提交的解决方案。
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0, user-scalable=yes"/>
<!-- From: -->
<title> Table Food Choices </title>
<!-- link rel="stylesheet" href="common.css" media="screen" -->
<style>
</style>
<body>
<form>
<h3> From each row: </h3>
<table id="tbl">
<thead>
<tr>
<th colspan="3">Select 2 vetables</th>
<th>And your favorite fruit:</th>
</tr>
</thead>
<tbody>
<tr>
<td> <label><input type="checkbox" class="checks" value ="Beans">Green Bean</label></td>
<td> <label><input type="checkbox" class="checks" value ="Corn">Corn</label></td>
<td> <label><input type="checkbox" class="checks" value ="Carrot">Carrot</label></td>
<th>
<select>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="grape">Grape</option>
<option value="orange">Orange</option>
<option value="peach">Peach</option>
<option value="pineapple">Pineapple</option>
<option value="tangerine">Tangerine</option>
</select>
</th>
</tr>
<tr>
<td> <label><input type="checkbox" class="checks" value ="Beans">Green Bean</label></td>
<td> <label><input type="checkbox" class="checks" value ="Corn">Corn</label></td>
<td> <label><input type="checkbox" class="checks" value ="Carrot">Carrot</label></td>
<th>
<select>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="grape">Grape</option>
<option value="orange">Orange</option>
<option value="peach">Peach</option>
<option value="pineapple">Pineapple</option>
<option value="tangerine">Tangerine</option>
</select>
</th>
</tr>
<tr>
<td> <label><input type="checkbox" class="checks" value ="Beans">Green Bean</label></td>
<td> <label><input type="checkbox" class="checks" value ="Corn">Corn</label></td>
<td> <label><input type="checkbox" class="checks" value ="Carrot">Carrot</label></td>
<th>
<select>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="grape">Grape</option>
<option value="orange">Orange</option>
<option value="peach">Peach</option>
<option value="pineapple">Pineapple</option>
<option value="tangerine">Tangerine</option>
</select>
</th>
</tr>
</tbody>
</table>
</form>
<input id="picks" type="submit" value="Submit choices">
<br>
Choices
<pre id='choices'></pre>
<script>
function run(){
var
tblRows = document.querySelectorAll('tbody tr'),
checks = document.querySelectorAll('.checks'),
selects = document.querySelectorAll('select'),
strs = []; // console.log(tblRows.length, checks.length, selects.length);
for (r = 0; r < tblRows.length; r++) {
let cbCount = 0, tstr = '';
for (cb=0; cb < 3; cb++) {
if ( checks[(r*3)+cb].checked ) {
cbCount++;
tstr += checks[(r*3)+cb].value + ' ';
}
}
if ( cbCount == 2 ) { strs.push(tstr + " " + selects[r].value); } // accept only 2 checkbox with 1 select values
}
document.getElementById('choices').textContent = strs.join('\n');
}
function init() {
document.getElementById('picks').addEventListener('click', run);
}
init();
</script>
</body>
</html>