2013-10-28 4 views
0

- EDIT--토글 버튼 - 하나를 클릭하여 모두 전환 토글 스위치

토글 버튼이 왼쪽과 오른쪽으로 슬라이드됩니다. 토글과 애니메이션은 정상적으로 작동하지만 같은 페이지에 두 개 이상의 토글 버튼 인스턴스를 추가 할 때 문제가 발생합니다. 토글 버튼을 클릭하면 모든 토글 버튼이 동일한 VAR을 사용하고 있습니다. 따라서 하나를 연 다음 다른 하나를 클릭하면 두 번째 슬라이드가 닫히지 않는다고 생각하기 때문에 슬라이드가 열리지 않습니다. isChecked var을 어디에 넣을 것인가? 각 인스턴스마다 다릅니 까?

JS 바이올린 http://jsfiddle.net/amQCN/11/

$(document).ready(function() { 


var isChecked = false; 
$('.toggle-radio-switch').click(function() { 
    if (isChecked == true) { 
     $(this).find('.radio-switch-slider').animate({'margin-left': '0px'},'150'); 
     isChecked = false; 
     console.log("isChecked = " + isChecked); 
    } else { 
     $(this).find('.radio-switch-slider').animate({'margin-left': '34px'},'150'); 
     isChecked = true; 
     console.log("isChecked = " + isChecked); 
    }; 
}); 


}); 

무선 스위치 슬라이더는 내용의 상단에 위치 앞뒤로 네 공개 슬라이드 또는 사람 넣다 더

<div class="toggle-radio-switch" id="toggle3"> 
    <span>yes</span> 
    <span>no</span> 
    <div class="radio-switch-slider"></div> 
</div> 

작업 버전이 궁금하지 않습니다 :

이 대신 사용하면 종료

$('.toggle-radio-switch').click(function() { 
    if ($(this).find('.radio-switch-slider').css('margin-left')=="0px"){ 
     $(this).find('.radio-switch-slider').animate({'margin-left': '34px'},'150'); 
    } else { 
     $(this).find('.radio-switch-slider').animate({'margin-left': '0px'},'150'); 
    } 
}); 
+0

에 직접 포함 - http://jsfiddle.net/amQCN/10/ – three7studio

답변

2

나는 .toggle-radio-switch 요소가 형제임을 배트하고 있습니다. 코드에서 .parent()을 제거하십시오. 그것은 .radio-switch-slider 때문에 필요하지 않습니다 내가 바이올린을 추가 .toggle-radio-switch

$(this).find('.radio-switch-slider')... 
+0

즉, 그것이 ... 그래서 분명 당신이 지적 후 다야 ,하지만 각각은 개별적으로 작동하기 때문에 var는 로컬로 설정되지 않으므로 모든 토글은 동일한 var = /이 스크립트에서 각 토글에 대해 로컬로 설정 하시겠습니까? – three7studio

+1

'data' http://api.jquery.com/data/를 사용하여 요소 자체에 저장할 수 있습니다. 또는 단순히'margin'의 현재 값을 검사하여 어떤 상태인지 확인할 수 있습니다. –

+0

감사합니다. james가 작동했습니다. .. 단지 var를 벗 겼고 마린을 확인했습니다. – three7studio