如何解决开/关切换javascript

时间:2013-05-29 09:01:22

标签: javascript html

我在on / off开关中有问题。我有一个代码,它无法正常工作,因为它在我写的形式检查时工作,但我想要两个心情,开关,什么时候不在它的形式检查写入它不起作用,当我写入两个检查时,它不起作用,当我写入其中一个检查它工作但不正确。:(

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

2 个答案:

答案 0 :(得分: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>

您可以查看 demo here 。如果它有问题或我忘了做某事,请通知我,我将更新演示。

使用此功能,您只需在复选框上使用onchange = DisEn(this)即可为每个复选框设置。

答案 1 :(得分: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 &gt;&gt; http://jsfiddle.net/YgWku/