0

当第一个单选按钮被选中时,我需要所有三个复选框来检查和禁用。

When the second or third radio button other_1is selected, I need the three checkboxes to uncheck and enable again.

代码(无效):

function validate_Days() {

var noOfDays = 0;   

if (document.classic.r.value = "3daypass") {
    noOfDays = 3;
}

if (document.classic.r.value = "2daypass") {
    noOfDays = 2;
}

if (document.classic.r.value = "1daypass") {
    noOfDays = 1;
}


if (noOfDays == 3) {
    document.getElementById('cb1').checked = true;
    document.getElementById('cb2').checked = true;
    document.getElementById('cb3').checked = true;
}
else if (noOfDays == 2) {
    document.getElementById('cb1').checked = false;
    document.getElementById('cb2').checked = false;
    document.getElementById('cb3').checked = false;
}
else if (noOfDays == 1) {
    document.getElementById('cb1').checked = false;
    document.getElementById('cb2').checked = false;
    document.getElementById('cb3').checked = false;
}
}

编辑:删除禁用并使用更有用的名称/值

4

1 回答 1

1

我不明白代码在做什么,但这是我将如何检查和取消选中复选框的方法。我也不明白为什么它们在选中时被禁用-禁用的控件不能成功,因此在提交表单时不会发布到服务器。

<script>
function getRadioValue(radioSet) {

  for (var i=0, iLen=radioSet.length; i<iLen; i++) {

    if (radioSet[i].checked) {
      return radioSet[i].value;
    }
  }
}

function updateRadios(el) {
    var form = el.form;
    var checked = getRadioValue(form.rb) == '3'? true : false;
    form.r1.checked = checked;
    form.r2.checked = checked;
    form.r3.checked = checked;
}
</script>

<form>
  <fieldset onclick="updateRadios(this)">
     <legend>Radio buttons</legend>
     <input type="radio" value="1" name="rb"> 1<br>
     <input type="radio" value="2" name="rb"> 2<br>
     <input type="radio" value="3" name="rb"> 3<br>
   </fieldset>
   <fieldset>
     <legend>Checkboxes</legend>
     <input type="checkbox" value="1" name="r1"> 1<br>
     <input type="checkbox" value="2" name="r2"> 2<br>
     <input type="checkbox" value="3" name="r3"> 3<br>
   </fieldset>
 </form>
于 2013-01-14T11:44:59.103 回答