【发布时间】:2017-04-06 20:26:59
【问题描述】:
我知道当有多个复选框时,我可以使用 jQuery (how to get multiple checkbox value using jquery) 来获取复选框值,但我的复选框输入位于 html 表单中,因此这些 jQuery 解决方案无法正常工作,因为它们都没有获得复选框值从一个表格内。
我尝试从表单中提取值,但它只是创建了一个奇怪的无线电节点列表,它似乎计算了复选框名称出现在文档中的次数而不是值。
function validateForm() {
var checks = document.forms["TestForm"]["ParticipantSelection[]"];
alert(checks);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<form name="TestForm" action="Video1.php" method="post" onsubmit="return validateForm()">
<table>
<tr>
<th><img name="<?php echo $valueindicator[0];?>" src="<?php echo $all_four_images[0];?>" height="100">
</th>
<th><img name="<?php echo $valueindicator[1];?>" src="<?php echo $all_four_images[1];?>" height="100">
</th>
<th><img name="<?php echo $valueindicator[2];?>" src="<?php echo $all_four_images[2];?>" height="100">
</th>
</tr>
<tr>
<th> <input type="checkbox" name="ParticipantSelection[]" value="image1">
</th>
<th><input type="checkbox" name="ParticipantSelection[]" value="image2">
</th>
<th><input type="checkbox" name="ParticipantSelection[]" value="image3">
</th>
</tr>
</table>
<input type="hidden" name="AnswerCondition" value="BrowserCheckAnswer">
<button type="submit">Continue</button>
</form>
但我不知道如何让 js 获取表单输入中内容的值而不是计算其他内容,也不知道如何访问复选框的值
【问题讨论】:
-
如果您想要它的价值,请尝试使用
.value。与您的document.forms类似。 -
嗨- 非常感谢您的回复!我确实尝试了几次 - 它返回 undefined 因为 html 复选框名称(这是我需要发布数据的东西)。
标签: javascript html forms checkbox nodelist