尝试禁用单选按钮时javascript无法正常工作

时间:2017-02-07 07:33:48

标签: javascript html forms

为什么我的js不工作,我错过了什么? 我点击收音机时试图禁用收音机 但它不起作用

希望有人能在这里帮助我 谢谢



  function tester() {
    for (var i = 0; i < document.forms[0].elements.length; i++) {
      if (document.forms[0].elements[i].name == "optradio1") {
        if (document.forms[0].elements[i].value == "pinjdn") {
          if (document.forms[0].elements[i].checked == true) {

            document.forms[0].soptradio[0].disabled = true;
            document.forms[0].soptradio[1].disabled = true;
            document.forms[0].soptradio[2].disabled = true;
            document.forms[0].soptradio[3].disabled = true;
          }
        } else if (document.forms[0].elements[i].value == "hibahln") {
          if (document.forms[0].elements[i].checked == true) {
            document.forms[0].soptradio[0].disabled = false;
            document.forms[0].soptradio[1].disabled = false;
            document.forms[0].soptradio[2].disabled = false;
            document.forms[0].soptradio[3].disabled = false;
          }
        }
      }
    }
  }
&#13;
<div class="col-sm-6 well">
  <div class="row">
    <form name="form1" method="post" action="">
      <table class="table table-striped">
        <tr>
          <th>Beban</th>
          <th>
            <label>
              <input type="radio" name="optradio1" value="rm" id="hide">RM</label>
            <label>
              <input type="radio" name="optradio1" value="pinjln" id="show">Pinj LN</label>
            <label>
              <input type="radio" name="optradio1" value="localcost" id="show2">Local Cost</label>
            <label>
              <input type="radio" name="optradio1" value="pnbp" id="hide2">PNBP</label>
            <label>
              <input type="radio" name="optradio1" value="pinjdn" id="show3" onClick="tester()">Pinj DN</label>
            <label>
              <input type="radio" name="optradio1" value="hibahdn" id="hide3">Hibah DN</label>
            <label>
              <input type="radio" name="optradio1" value="hibahln" id="show4" onClick="tester()">Hibah LN</label>
          </th>
        </tr>
      </table>
    </form>
  </div>
  <div class="row">
    <div class="abc">
      <div class="col-sm-12">


        <form name="form1" method="post" action="">
          <table class="table table-striped">
            <tr>
              <div class="radio">
                <th>Jenis Bantuan</th>
                <th>
                  <label>
                    <input type="radio" name="soptradio">Pinjaman Valas</label>
                  <label>
                    <input type="radio" name="soptradio" checked>Pinj. RPLN</label>
                  <label>
                    <input type="radio" name="soptradio">Hibah Valas</label>
                  <label>
                    <input type="radio" name="soptradio">Hibah RPLN</label>
                </th>
              </div>
            </tr>
          </table>
        </form>
&#13;
&#13;
&#13;

这是我的代码 请帮帮我

希望我的解释对每个人都清楚

谢谢

1 个答案:

答案 0 :(得分:0)

您必须在禁用时参考表单[1](第二种形式)。请参阅以下更改的代码:

&#13;
&#13;
     function tester()
     {
    for(var i=0;i<document.forms[0].elements.length;i++)
      {
        if(document.forms[0].elements[i].name=="optradio1")
        {
         if(document.forms[0].elements[i].value=="pinjdn")
         {
           if(document.forms[0].elements[i].checked==true){
     
            document.forms[1].soptradio[0].disabled=true;
            document.forms[1].soptradio[1].disabled=true;
            document.forms[1].soptradio[2].disabled=true;
     		document.forms[1].soptradio[3].disabled=true;
           }
         }
          else if(document.forms[0].elements[i].value=="hibahln")
         {
           if(document.forms[0].elements[i].checked==true){
            document.forms[1].soptradio[0].disabled=false;
            document.forms[1].soptradio[1].disabled=false;
            document.forms[1].soptradio[2].disabled=false;
     		document.forms[1].soptradio[3].disabled=false;
    	}
    	}
    }
    }
     }
&#13;
<div class="col-sm-6 well">
        <div class="row">
            <form name="form1" method="post" action="">
            <table class="table table-striped">
                <tr>
                    <th>
                        Beban
                    </th>
                    <th>
                        <label>
                            <input type="radio" name="optradio1" value="rm" id="hide">RM</label>
                        <label>
                            <input type="radio" name="optradio1" value="pinjln" id="show">Pinj LN</label>
                        <label>
                            <input type="radio" name="optradio1" value="localcost" id="show2">Local Cost</label>
                        <label>
                            <input type="radio" name="optradio1" value="pnbp" id="hide2">PNBP</label>
                        <label>
                            <input type="radio" name="optradio1" value="pinjdn" id="show3" onclick="tester()">Pinj
                            DN</label>
                        <label>
                            <input type="radio" name="optradio1" value="hibahdn" id="hide3">Hibah DN</label>
                        <label>
                            <input type="radio" name="optradio1" value="hibahln" id="show4" onclick="tester()">Hibah
                            LN</label>
                    </th>
                </tr>
            </table>
            </form>
        </div>
        <div class="row">
            <div class="abc">
                <div class="col-sm-12">
                    <form name="form1" method="post" action="">
                    <table class="table table-striped">
                        <tr>
                            <div class="radio">
                                <th>
                                    Jenis Bantuan
                                </th>
                                <th>
                                    <label>
                                        <input type="radio" name="soptradio">Pinjaman Valas</label>
                                    <label>
                                        <input type="radio" name="soptradio" checked>Pinj. RPLN</label>
                                    <label>
                                        <input type="radio" name="soptradio">Hibah Valas</label>
                                    <label>
                                        <input type="radio" name="soptradio">Hibah RPLN</label>
                                </th>
                            </div>
                        </tr>
                    </table>
                    </form>
                </div>
            </div>
        </div>
    </div>
&#13;
&#13;
&#13;