試圖把一個已經樣式等的按鈕,使得當在其上的用戶點擊,顏色會根據改變無線電輸入的背景顏色到無線電價值。
預期的結果
當點擊按鈕「中性」,改變按鈕的背景顏色爲白色。
點擊按鈕「GOOD」後,將按鈕背景顏色更改爲綠色。
當點擊「WATCHLIST」按鈕時,將按鈕背景更改爲黃色。
當點擊「UNDER MONOTORING」按鈕時,將按鈕背景改爲紅色。
問題
它不會相應地改變按鈕的背景,這就是我想要做的事。
在此欣賞一些幫助。預先感謝。
$(document).ready(function() {
$('label').click(function() {
if ($("input:radio[name='category']:checked").val() == 'W') {
$(this).addClass('whiteBG');
}
if ($("input:radio[name='category']:checked").val() == 'G') {
$(this).addClass('greenBG');
}
if ($("input:radio[name='category']:checked").val() == 'Y') {
$(this).addClass('yellowBG');
}
if ($("input:radio[name='category']:checked").val() == 'R') {
$(this).addClass('redBG');
}
});
});
input[type=radio],
input[type=checkbox] {
display: none;
}
label {
display: block;
appearance: button;
-webkit-appearance: button;
-moz-appearance: button;
-ms-appearance: button;
font-family: 'Roboto', sans-serif;
font-weight: 400;
background: #DDDDDD;
font-size: 1.6rem;
color: #111111;
border: 2px solid #AAAAAA;
padding: 8px;
width: 40%;
margin: 0 auto;
text-align: center;
transition: all 0.7s ease-in-out;
}
.whiteBG {
background-color: #FFF000;
}
.greenBG {
background-color: #0F0000;
}
.yellowBG {
background-color: #FF0000;
}
.redBG {
background-color: #F00000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
<fieldset data-role="controlgroup">
<legend>PERSON CATEGORY SELECTION</legend>
<label for="neutral">
<input type="radio" class="button" id="neutral" name="category" value="W" >NEUTRAL</input>
</label>
<label for="good">
<input type="radio" class="button" id="good" name="category" value="G">GOOD</input>
</label>
<label for="watchlist">
<input type="radio" class="button" id="watchlist" name="category" value="Y">WATCHLIST</input>
</label>
<label for="monitor">
<input type="radio" class="button" id="monitor \t " name="category" value="R">UNDER MONOTORING</input>
</label>
</fieldset>
</form>
你是說點擊其中一個按鈕應該將*那個*按鈕的背景色改成指定的顏色,並將其他按鈕改回灰色? – nnnnnn
是的,其他人應該變回灰色。 – SiChiPan