简体   繁体   English

切换按钮 - 单击一个和所有切换开关

[英]toggle button - click one and all toggles switch

--EDIT-- - 编辑 -

I have the toggle Button with which slides left and right. 我有左右滑动的切换按钮。 The toggle and animation are working fine, but the problem comes when adding more than one instance of toggle button on the same page. 切换和动画工作正常,但在同一页面上添加多个切换按钮实例时会出现问题。 When I click on a toggle button, All the toggle buttons are using the same VAR. 当我单击切换按钮时,所有切换按钮都使用相同的VAR。 So if you open one and then click another one, the second one doesn't slide open because it thinks is suppose to close. 因此,如果您打开一个然后单击另一个,则第二个不会滑动打开,因为它认为是关闭。 Where would you put the isChecked var so that it is different for each instance? 你会把isChecked var放在哪里,以便每个实例都不同?

js fiddle http://jsfiddle.net/amQCN/11/ 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);
    };
});


});

radio-switch-slider is positioned on top of the contents and slides back and forth revealing yes or no 无线电开关滑块位于内容的顶部,来回滑动显示是或否

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

Working version incase anyone is wondering: 任何人都想知道工作版本:

Ended up just using this instead 结束使用它而不是

$('.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');
    }
});

I'm betting your .toggle-radio-switch elements are siblings. 我打赌你的.toggle-radio-switch元素是兄弟姐妹。 Remove .parent() from your code. 从代码中删除.parent() It isn't needed since .radio-switch-slider is contained directly in .toggle-radio-switch 由于.radio-switch-slider直接包含在.toggle-radio-switch ,因此.toggle-radio-switch

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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