0

问题:我想在我的单选按钮上进行三向切换。

示例:全部灰色启动,第一次单击按钮 1 将其变为绿色,第二次单击 SAME 按钮 1 将其更改为桃色,随后的任何单击都会在这两种状态之间循环。单击任何其他按钮会将其重置为原始状态。

我有三个单选类型切换按钮。只有一个按钮可以像收音机一样选择时间。此外,如果再次单击任何选定的按钮,它将取消选中。我正在尝试在其中添加更多内容。

我想要这样的东西:

最初按钮是灰色的。

如果单击任何按钮,则该按钮会从绿色变为灰色。再次单击该选定按钮将变回灰色。

但是我最初想要的是所有按钮都将保持灰色,但是如果我先单击任何一个,它将变成绿色,然后再次单击相同的按钮将给出另一种颜色(可以是任何颜色)。再次单击相同的按钮将变回绿色并将继续.. 但是如果我单击另一个按钮上一个按钮将更改为灰色和当前按钮可以做同样的事情。

这是我的大学项目之一的要求。但是找不到这样的方法。

应保持当前功能。希望我清楚要求。如果可以做到这一点,那对我来说会很棒。

这是当前示例的链接:JS Fiddle

HTML:

<div style="margin:0px auto; margin-top:100px; width:960px;">
    <a href="#">
        <span class="toggleButtonRadio normal">toggle Radio button</span>
    </a>


    <a href="#">
        <span class="toggleButtonRadio normal">toggle Radio button</span>
    </a>


    <a href="#">
        <span class="toggleButtonRadio normal">toggle Radio button</span>
    </a>
</div>

CSS

* {
 margin: 0;
 padding: 0;
 border: 0px;
}

body {
 background: #fafafa;
 font-family: Helvetica, Arial, sans-serif;
 font-size: 0.813em;
 color:#333333;


}

a {color:#737373;text-decoration:none;
}

a:link { text-decoration: none;outline: none;
}

a:visited {text-decoration: none;
}

a:hover {color:#454545;
}

a:active {text-decoration: none;
}

ul {list-style: none; text-decoration: none;
}

ol {list-style-position: inside; color:#333333;padding:12px 10px 25px 15px;font-size: 1.07em;}
ol, li {padding-bottom:8px;}

img {}
em {color: #737373; font-weight:300;}

h1 {color: #737378; margin:20px 5px 20px 0px; font-size:2.4em; text-transform:uppercase;letter-spacing:1px; font-weight:100; text-shadow:1px 1px 1px #fff; }
h2 {color: #454545; margin:10px 5px 0px 0px;}
h3 {color: #454545; margin:10px 5px 0px 0px;}
h4 {color: #454545; margin:10px 5px 20px 0px; font-size:1.2em; width:98%; border-bottom:1px solid #eee;padding-bottom:10px;}
h5 {color: #454545; margin:10px 5px 0px 0px;}
h6 {color: #454545; margin:10px 5px 0px 0px;}

p { color:#333; font-size:1.154em; margin:5px 10px 10px 0px; padding-bottom:10px;line-height:140%;}

hr { border: 0; clear: both; display: block; width: 100%; background-color: #bbbbbb; height: 1px; margin: 5px 0px;
}

select {
 color: #454545;
}

.toggleButtonRadio , .toggleButton {
 background: #e1e1e1; 
 text-decoration: none;
 text-align: center;
 font-family: Helvetica, Arial, sans-serif;
 font-size: .769em;
 font-weight: 900;
 color: #454545;
 text-transform: uppercase;
 text-decoration: none;
 padding: 5px 10px;
 -webkit-border-radius: 15px;
 border-radius: 15px;
 -webkit-box-shadow: 0px 0px 3px 0px rgba(64, 64, 64, .5);
box-shadow: 0px 0px 3px 0px rgba(64, 64, 64, .5);
}

.toggleButtonRadio:hover, .toggleButton:hover{
 background:#d4d4d4;
}

.toggleButtonRadio.active , .toggleButton.active{
background:#90bf00;
color:#fff;
}

.active:hover{
background:#7ca600;
}

.active2{

    background:#fcc79a;
}

JS

$('.toggleButtonRadio').click(function(){
    $('.toggleButtonRadio').not(this).removeClass("active");        
    $(this).toggleClass("active");


    });
4

1 回答 1

1

我认为这就是您要实现的目标?

你想第二次点击按钮来激活第二次活动类吗?

$('.toggleButtonRadio').click(function () {
    if ($(this).hasClass("normal")) {
        $(this).addClass("active");
        $(this).removeClass("normal");
    } else if ($(this).hasClass("active")) {
        $(this).addClass("active2");
        $(this).removeClass("active");
    } else if ($(this).hasClass("active2")) {
        $(this).addClass("active");
        $(this).removeClass("active2");
    }

    $('.toggleButtonRadio').not(this).removeClass("active");
    $('.toggleButtonRadio').not(this).removeClass("active2");
    $('.toggleButtonRadio').not(this).addClass("normal");
});

http://jsfiddle.net/Mutmatt/HPC5y/

于 2013-11-08T15:47:49.470 回答