[英]Enable/disable a form element after checking radio button checked states
我对单选按钮有十个左右的问题。 在用户可以前进到另一个级别之前,都需要将它们都设置为true。 当且仅当这十个问题均已回答为真时,我才希望启用表单上的元素之一以进行进一步的编辑。 我可以在服务器端执行此操作,但是不知道如何在JavaScript中执行此操作。 有什么帮助吗? 非常感激。 谢谢。
<div>
<label> First Question:</label>
<label>
<input checked="checked" class="radio_buttons optional pull-right allowed" id="is_complete_and_works_true" name="project[person_attributes][is_complete_and_works]" type="radio" value="true" />Yes</label>
<label >
<input class="radio_buttons optional pull-right allowed" id="is_complete_and_works_false" name="project[person_attributes][is_complete_and_works]" type="radio" value="false" />No</label>
</div>
<div>
<label> Second Question:</label>
<label>
<input checked="checked" class="radio_buttons optional pull-right allowed" id="is_complete_and_works_true" name="project[person_attributes][researched]" type="radio" value="true" />Yes</label>
<label >
<input class="radio_buttons optional pull-right allowed" id="is_complete_and_works_false" name="project[person_attributes][researched]" type="radio" value="false" />No</label>
</div>
<div>
<label> Third Question:</label>
<label>
<input checked="checked" class="radio_buttons optional pull-right allowed" id="is_complete_and_works_true" name="project[person_attributes][is_complete_and_works]" type="radio" value="true" />Yes</label>
<label >
<input class="radio_buttons optional pull-right allowed" id="is_complete_and_works_false" name="project[person_attributes][is_complete_and_works]" type="radio" value="false" />No</label>
</div>
这段代码扩展了更多的问题。
通过这样做,我已经能够选择收音机:
var first_ten = $(':radio[name="project[person_attributes][is_complete_and_works]"][value=true], :radio[name="project[person_attributes][researched]"][value=true], etc…);
现在,我不知道如何遍历每个对象,当我单击每个单选按钮时(是或否),我想查看要启用的元素的结果。 任何想法表示赞赏。
像下面这样的事情会做的:
<script type="text/javascript">
function proceed(form) {
var el, els = form.getElementsByTagName('input');
var i = els.length;
while (i--) {
el = els[i];
if (el.type == 'checkbox' && !el.checked) {
form.proceedButton.disabled = true;
return;
}
}
form.proceedButton.disabled = false;
}
</script>
<form onclick="proceed(this);">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="submit" name="proceedButton" disabled>
</form>
请注意,这被认为是错误的设计,因为javascript不可用或未启用,用户永远无法单击该按钮。 最好以有用的状态交付表单,并在提交时使用脚本验证所有按钮均已选中,如果没有,则取消提交。
然后,在服务器上,您还可以检查状态,并且仅在当前页面通过验证时才显示下一页。 如果不是,请使用户返回首页。
这样,无论您还是用户都不关心脚本是否起作用,页面仍然可以运行。 当然,如果脚本能够正常工作,可能会是更好的体验,但是至少选择的不是二进制文件,它还为您提供了一个简单的后备支持,以最少的努力即可支持多种浏览器。
因此,更好的解决方案是:
<form onsubmit="reurn validate(this);" ...>
...
</form>
然后在函数中:
function validate(form) {
// if validateion fails, show an appropriate message and return false,
// if it passes, return undefined or true.
}
始终要在服务器上进行验证,因为您真的不知道客户端发生了什么。
表单控件不需要名称和ID,只需使用名称即可。 在单选按钮集中,只能检查一个控件,而不能检查所有控件。
在我看来,您要尝试做的是查看每组中是否至少检查了一个单选按钮。 您可以根据上面的代码进行操作,并在遇到时选择每个集合,例如
function validateForm(form) {
// Get all the form controls
var control, controls = form.elements;
var radios = {};
var t, ts;
// Iterate over them
for (var i=0, iLen=controls.length; i<iLen; i++) {
control = controls[i];
// If encounter a radio button in a set that hasn't been visited
if (control.type == 'radio' && !radios.hasOwnProperty(control.name)) {
ts = form[control.name];
radios[control.name] = false;
// Check the set to see if one is checked
for (var j=0, jLen=ts.length; j<jLen; j++) {
if (ts[j].checked) {
radios[control.name] = true;
}
}
}
}
// Return false if any set doesn't have a checked radio
for (var p in radios) {
if (radios.hasOwnProperty(p)) {
if (!radios[p]) {
alert('missing one');
return false;
}
}
}
}
</script>
<form onsubmit="return validateForm(this);">
<input type="radio" name="r0">
<input type="radio" name="r0">
<br>
<input type="radio" name="r1">
<input type="radio" name="r1">
<br>
<input type="reset"><input type="submit">
</form>
请注意,您的表单应包含一个重置按钮,尤其是在使用单选按钮时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.