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

时间:2012-07-29 23:50:06

标签: javascript jquery

我有10个左右的单选按钮问题。在用户可以移动到另一个级别之前,它们都需要设置为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…);

现在,我不知道如何迭代每个,当我点击每个收音机时,无论是或否,我想看看元素的启用结果。任何想法都非常感激。

1 个答案:

答案 0 :(得分:1)

以下内容将完成这项工作:

<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>

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