当检查单选输入时,如何将JS中的附加类添加到div或table中?
当我检查无线电输入时,我想将table-selected类添加到div class="table-item"中。
$(".table-item input").change(function() {
var div = $(this).closest('.table-item');
$(this).is(":checked") ? div.addClass("table-selected") : div.removeClass("table-selected");
});
#test .table-item {
border: solid 1px #666;
height: 25px;
background-color: #CCC
}
#test .table-selected {
border: 1 px solid #F00;
background-color: #00F
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="test">
<div class="table-item">
<div><label for="check1"><input type='radio' name='rad' value='1'>1</label></div>
</div>
<div class="table-item">
<div><label for="check2"><input type='radio' name='rad' value='2'>2</label></div>
</div>
<div class="table-item">
<div><label for="check3"><input type='radio' name='rad' value='3'>3</label></div>
</div>
</div>
但这工作有些错误,添加类,但在未选中时不会删除它
富国沪深
30秒到达战场
相关分类