在检查单选按钮检查状态后启用/禁用表单元素

Enable/disable a form element after checking radio button checked states

本文关键字:检查 元素 表单 启用 单选按钮 状态      更新时间:2024-05-28

我有十个左右关于单选按钮的问题。在用户可以进入另一个级别之前,它们都需要设置为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>

请注意,您的表单应该包含一个重置按钮,尤其是在使用单选按钮时。