试图把一个已经样式等的按钮,使得当在其上的用户点击,颜色会根据改变无线电输入的背景颜色到无线电价值。
预期的结果
当点击按钮“中性”,改变按钮的背景颜色为白色。
点击按钮“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