我試圖確定在單選按鈕組中單選按鈕組中的哪個單選按鈕。我的標記和CSS是爲這個特定的單選按鈕定製的,我認爲這可能會導致我的點擊事件發生問題。我在JavaScript中有一些條件邏輯,其中兩個警報都出現在點擊。任何人有關於如何糾正這個問題的想法?確定選擇哪個單選按鈕 - 自定義單選按鈕標記
這是一個Fiddle的問題。
標記
<div class="container">
<div class="row">
<div class="col-sm-6">
<div class="toggle" id="family-radio-btn">
<input type="radio" class="toggle-input" name="family" id="family-yes" checked>
<label for="family-yes" class="toggle-label toggle-label-off">Yes</label>
<input type="radio" class="toggle-input" name="family" id="family-no">
<label for="family-no" class="toggle-label toggle-label-on">No</label>
<span class="toggle-selection"></span>
</div>
</div>
</div>
</div>
JS
$('#family-radio-btn').click(function() {
if ($('#family-yes:checked').length) {
alert("yes");
}
else
{
alert("no");
}
});
CSS
.toggle {
position: relative;
height: 34px;
width: 100%;
margin: 0;
background: #ffffff;
border: 1px solid #cccccc;
border-radius: 3px;
}
.toggle .toggle-input {
display: none;
}
.toggle .toggle-input:checked + .toggle-label {
color: #ffffff;
}
.toggle .toggle-input:checked + .toggle-label-on ~ .toggle-selection {
left: 49%;
}
.toggle .toggle-label {
position: relative;
z-index: 2;
float: left;
width: 50%;
line-height: 32px;
color: #555555;
text-align: center;
font-weight: normal;
cursor: pointer;
}
.toggle .toggle-label.toggle-label-off {
padding-left: 2px;
}
.toggle .toggle-label.toggle-label-on {
padding-right: 2px;
}
.toggle .toggle-selection {
position: absolute;
z-index: 1;
top: 2px;
left: 2px;
display: block;
width: 50%;
height: 28px;
border-radius: 3px;
background-color: #0071bc;
-webkit-transition: left 0.15s ease-out;
-moz-transition: left 0.15s ease-out;
-ms-transition: left 0.15s ease-out;
-o-transition: left 0.15s ease-out;
transition: left 0.15s ease-out;
}
訣竅,謝謝。那麼,你修改的點擊事件如何糾正這個問題? – noclist
而不是選擇'div',我們需要獲得'input'的'click'事件 – RonyLoud