简体   繁体   中英

how to solve on/off switch javascript

I have a problem in on/off switch.i have a code which it doesn't work correctly,because it works when i write checked in its form,but i want tow mood,on and off,when don't writ checked in its form it doesn't work and when i write checked in both,it doesn't work,when i write checked in one of them it works but not correctly.:(

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

Use onchange = DisEn(this) like this example:

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

You can check demo here . If it has something wrong or I forgot to do something, notify and I will update demo.

Using this, you will be able to set it for every checkbox, just using onchange = DisEn(this) on your checkboxes.

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


}  

Here is the jsfiddle >> http://jsfiddle.net/YgWku/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM