gpt4 book ai didi

javascript - 在 HTML 表格中获取整个选定的列值

转载 作者:行者123 更新时间:2023-12-04 07:35:25 26 4
gpt4 key购买 nike

我在获取 Checkbox 时遇到问题和 Company HTML 表格列中的整个值,下面是示例图片:
output1
现在我只能获取第一行的值,警报消息显示 Checkbox value is: true and Company value is: Alfreds Futterkiste .
我可以知道如何获得 Checkbox 的全部值吗?和 Company在下面的表格列中,我想要预期的结果,警报消息将显示 Checkbox value is: true,false,true,false,true,false and Company value is: Alfreds Futterkiste,Centro comercial Moctezuma,Ernst Handel,Island Trading,Laughing Bacchus Winecellars,Magazzini Alimentari Riuniti :
output2

function show_value(){
var checkbox_val = document.getElementById("checkbox_val");
var company_name = document.getElementById("company_name").value;
alert ("Checkbox value is: " + checkbox_val.checked + " and Company value is: " + company_name);
//alert ("Company Name value is: " + company_name);
}
https://stackoverflow.com/questions/ask#
<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>

<h2>HTML Table</h2>

<table>
<tr>
<th>Checkbox</th>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td><input type="checkbox" id="checkbox_val" name="checkbox_val" value="0"/>&nbsp;</td>
<td><input type="text" id="company_name" name="company_name" value="Alfreds Futterkiste"/></td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td><input type="checkbox" id="checkbox_val" name="checkbox_val" value="0"/>&nbsp;</td>
<td><input type="text" id="company_name" name="company_name" value="Centro comercial Moctezuma"/></td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td><input type="checkbox" id="checkbox_val" name="checkbox_val" value="0"/>&nbsp;</td>
<td><input type="text" id="company_name" name="company_name" value="Ernst Handel"/></td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td><input type="checkbox" id="checkbox_val" name="checkbox_val" value="0"/>&nbsp;</td>
<td><input type="text" id="company_name" name="company_name" value="Island Trading"/></td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td><input type="checkbox" id="checkbox_val" name="checkbox_val" value="0"/>&nbsp;</td>
<td><input type="text" id="company_name" name="company_name" value="Laughing Bacchus Winecellars"/></td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td><input type="checkbox" id="checkbox_val" name="checkbox_val" value="0" />&nbsp;</td>
<td><input type="text" id="company_name" name="company_name" value="Magazzini Alimentari Riuniti"/></td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
<button type="button" id="updateBtn_5" class="btn btn-sm btn-primary" onclick="show_value()">Show checkbox and company value</button>
</body>
</html>

希望有人能指导我如何解决它。谢谢。

最佳答案

您可以 each循环遍历您的 trs然后在这个内部使用 .val() 获取输入值和 is(":checked")并将它们保存在 array 中。
演示代码 :

function show_value() {
var checkbox_state = []
var company_name = []
//use each loop
$("[name=checkbox_val]").each(function() {
checkbox_state.push($(this).is(":checked"))
company_name.push($(this).closest("tr").find("[name=company_name]").val()) //push value in array

})
//just to print..
console.log("checkbox -- " + checkbox_state.toString() + " Company name --" + company_name.toString())

}
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #dddddd;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<table>
<tr>
<th>Checkbox</th>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td><input type="checkbox" name="checkbox_val" value="0" />&nbsp;</td>
<td><input type="text" name="company_name" value="Alfreds Futterkiste" /></td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox_val" value="0" />&nbsp;</td>
<td><input type="text" name="company_name" value="Centro comercial Moctezuma" /></td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox_val" value="0" />&nbsp;</td>
<td><input type="text" name="company_name" value="Ernst Handel" /></td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td><input type="checkbox" id="checkbox_val" name="checkbox_val" value="0" checked/>&nbsp;</td>
<td><input type="text" id="company_name" name="company_name" value="Island Trading" /></td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox_val" value="0" checked />&nbsp;</td>
<td><input type="text" name="company_name" value="Laughing Bacchus Winecellars" /></td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
</table>
<button type="button" id="updateBtn_5" class="btn btn-sm btn-primary" onclick="show_value()">Show checkbox and company value</button>

关于javascript - 在 HTML 表格中获取整个选定的列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67775263/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com