簡體   English   中英

單選按鈕切換問題

[英]Radio Toggle Button Issue

我有一個單選按鈕。 有三個按鈕,並且一次只能選擇一個按鈕的選項。 但是問題是按鈕始終保持選中狀態(我知道單選按鈕沒有取消選中的選項)。 沒有“全部取消選中”的選項。

我想做這樣的事情:

如果再次單擊任何選定的按鈕,它將取消選中。 但是,如果不單擊所選按鈕,它將與當前按鈕相同。

這有可能嗎? 對不起,英語不好

這是演示鏈接: 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;
}

JS

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


    });

我已經通過一個簡單的IF ELSE陳述解決了您的問題:

JQuery的

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

這應該做您想做的,因為它會刪除所有同級上的active類(因此.not(this) 。然后,單擊或關閉按鈕的active類。

編輯:更改$(this)this ,它更簡單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM