【发布时间】:2020-12-04 09:40:18
【问题描述】:
我在回答这个问题时说我对 JavaScript 的了解非常有限,而且我不是开发人员。我被要求帮助为我的公司构建价格计算器向导。
我从 W3 Schools (https://www.w3schools.com/howto/howto_js_form_steps.asp) 找到了一些代码,用于创建一个检查每个输入字段是否为空的表单。我想更改代码以检查是否选择了每个组中的单选按钮。
我不知道如何更改这行代码
if (y[i].value == "")
检查单选按钮是否被选中。
下面是我的一个广播组和检查输入字段是否为空的脚本。
如果您想查看完整的代码,请转到上面的 W3 Schools 链接。
<div class="tab">Option 1:
<p><input type="radio" id="male" name="gender" value="male" oninput="this.className = ''"><label for="male">Male</label></p>
<p><input type="radio" id="female" name="gender" value="female" oninput="this.className = ''"><label for="female">Female</label></p>
</div>
function validateForm() {
// This function deals with validation of the form fields
var x, y, i, valid = true;
x = document.getElementsByClassName("tab");
y = x[currentTab].getElementsByTagName("input");
// A loop that checks every input field in the current tab:
for (i = 0; i < y.length; i++) {
// If a field is empty...
if (y[i].value == "") {
// add an "invalid" class to the field:
y[i].className += " invalid";
// and set the current valid status to false:
valid = false;
}
}
// If the valid status is true, mark the step as finished and valid:
if (valid) {
document.getElementsByClassName("step")[currentTab].className += " finish";
}
return valid; // return the valid status
}
【问题讨论】:
-
我不知道你在说什么。我要问的是如何更改代码链接以检查单选按钮是否被选中,而不是检查输入字段是否包含值。
-
您的问题模棱两可。你想要什么 ??检查所有单选按钮或验证某些值??
-
@tuhin47 你很有帮助
标签: javascript validation radio-button