【发布时间】:2017-08-16 03:52:41
【问题描述】:
目标
尝试更改样式为按钮的单选输入的背景颜色,以便当用户单击它时,颜色将根据单选值更改。
预期结果
当点击“NEUTRAL”按钮时,将按钮背景颜色更改为白色。
单击“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 " name="category" value="R">UNDER MONOTORING</input>
</label>
</fieldset>
</form>
【问题讨论】:
-
您是说单击其中一个按钮应该将那个按钮的背景颜色更改为指定颜色,并将其他按钮更改回灰色?
-
是的,其他的应该变回灰色。
标签: javascript jquery html css