0

我的开/关开关有问题。我有一个代码不能正常工作,因为当我以它的形式写检查时它工作,但我想拖着心情,打开和关闭,当不写检查时它的形式不起作用,当我在两者中都写检查时,它不起作用,当我在其中一个中写检查时,它工作但不正确。:(

<script>  
function DisEn(X){  
  document.A.T1.disabled=X;
  document.A.T2.disabled=X;

}  
</script>
</head>
 <form name="A" method="get" action="switch.php">
    <div class="onoffswitch">
<input type="checkbox" name="onoffswitch" value="off" class="onoffswitch-checkbox" 
        id="myonoffswitch" onClick="DisEn(false)" checked>
   <input type="checkbox" name="onoffswitch" value="on" class="onoffswitch-checkbox" 
   id="myonoffswitch1" onClick="DisEn(false)" >

<label class="onoffswitch-label" for="myonoffswitch">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>

</div>
<input type="text" name="T1" value="A" disabled>  
  <input type="text" name="T2" value="B" disabled>

<input type="submit" name="T4" value="Submit" >   
</form>
4

2 回答 2

1

onchange = DisEn(this)像这个例子一样使用:

<script>  
    function DisEn(element){  
       if (element.checked === true) {
          document.A.T1.disabled=true;
          document.A.T2.disabled=true;
          element.value="off";
       } else {
          document.A.T1.disabled=false;
          document.A.T2.disabled=false;
          element.value="on";
       }
    }  
</script>
<form name="A" method="get" action="switch.php">
    <div class="onoffswitch">
        <input type="checkbox" name="onoffswitch" value="off" 
            class="onoffswitchcheckbox" id="myonoffswitch"
            onchange="DisEn(this)" checked = "checked" />
        <label class="onoffswitch-label" for="myonoffswitch">
            <div class="onoffswitch-inner"></div>
            <div class="onoffswitch-switch"></div>
        </label>
    </div>
    <input type="text" name="T1" value="A" disabled = "disabled" />
    <input type="text" name="T2" value="B" disabled = "disabled" />
    <input type="submit" name="T4" value="Submit" />
</form>

你可以在这里查看演示。如果它有问题或我忘了做某事,通知我会更新演示。

使用它,您将能够为每个复选框设置它,只需onchange = DisEn(this)在您的复选框上使用。

于 2013-05-29T09:13:21.463 回答
0

HTML:

<form name="A" method="get" action="switch.php">
    <div class="onoffswitch">
<input type="checkbox" name="onoffswitch" value="off"class="onoffswitchcheckbox"    
   id="myonoffswitch" onClick="DisEn()" checked> 


<label class="onoffswitch-label" for="myonoffswitch">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>

</div>
<input type="text" name="T1" value="A" disabled>  
  <input type="text" name="T2" value="B" disabled>

 <input type="submit" name="T4" value="Submit" >      
 </form>

Javascript:

function DisEn(){  

    if(document.A.onoffswitch.checked == false)
    {
  document.A.T1.disabled=false;
  document.A.T2.disabled=false;
    }
    else
    {
  document.A.T1.disabled=true;
  document.A.T2.disabled=true;
    }


}  

jsfiddle >> http://jsfiddle.net/YgWku/

于 2013-05-29T09:13:07.727 回答