2014-12-11 2 views
-1

를 사용하여 스위치를 플립 : 스위치 1이 ON 위치에 있다면 ... 내가 그들의 상태는 항상 상대방의 반대 싶습니다내가 두 개의 스위치가 자바 스크립트

<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="switch1" checked> 
<label class="onoffswitch-label" for="switch1"> 
    <span class="onoffswitch-inner"></span> 
    <span class="onoffswitch-switch"></span> 
</label> 

<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="switch2" checked> 
<label class="onoffswitch-label" for="switch2"> 
    <span class="onoffswitch-inner"></span> 
    <span class="onoffswitch-switch"></span> 
</label> 

를, 스위치 2는 자동으로 OFF로 전환 위치, 그 반대의 경우도 마찬가지입니다.

어떻게하면 자바 스크립트를 사용하여이 작업을 수행 할 수 있습니까?

감사합니다.

+0

질문에 플러그인 (이름)/스위치를 구성하는 데 사용하는 스크립트를 포함 해주세요 (클릭 [편집] (http://stackoverflow.com/posts/27417801/edit) 게시물을 수정하려면 ...) – webeno

답변

0

하나만 사용하려면 확인란을 라디오로 변경하십시오.

0 또는 하나만 원할 경우 각 하나에 대해 onchange를 추적하고 이에 따라 다른 하나를 변경해야합니다.

제로의 예 또는 단 하나 :

<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="switch1" onchange="switch1hecked()" > 
<label class="onoffswitch-label" for="switch1"> 
    <span class="onoffswitch-inner"></span> 
    <span class="onoffswitch-switch"></span> 
</label> 

<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="switch2" onchange="switch2hecked()" > 
<label class="onoffswitch-label" for="switch2"> 
    <span class="onoffswitch-inner"></span> 
    <span class="onoffswitch-switch"></span> 
</label> 

<script> 
    function switch1hecked() { 
     if (document.getElementById('switch1').checked) 
      document.getElementById('switch2').checked = false; 
    } 

    function switch2hecked() { 
     if (document.getElementById('switch2').checked) 
      document.getElementById('switch1').checked = false; 
    } 
</script> 
관련 문제