我想在用户单击第一个单选按钮时执行一个操作,它还会检测其他四个未选中的单选按钮。我面临一个挑战,我不能一个一个地使用 if(!("...").is(':checked') ,任何其他方式来实现我需要的东西,谢谢?
$('#first').click(function() {
if ($('#first').is(':checked')) {
alert('checked');
}
//I want to do an action here for the other four radio button like second alert('no check'), third alert('no check')....
// but could not use this way
//if (!$('#second').is(':checked')) one by one. any other way?
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="cureway" id="first" class="checker" />first
<input type="radio" name="cureway" id="second" class="checker" />second
<input type="radio" name="cureway" id="third" class="checker" />third
<input type="radio" name="cureway" id="forth" class="checker" />forth
<input type="radio" name="cureway" id="five" class="checker" />five
最佳答案
您可以使用 :not(:checked)
选择器来查找未选中的单选按钮,如下所示,并使用 .each
遍历它们。
$('input[name="cureway"]').click(function() {
//clicked button get selected by default so no need to check
// if checked or not
alert("checked button is : "+ this.id);
//all other unchecked buttons
$('input[name="cureway"]:not(:checked)').each(function(){
alert("Unchecked button is : "+this.id);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="cureway" id="first" class="checker" />first
<input type="radio" name="cureway" id="second" class="checker" />second
<input type="radio" name="cureway" id="third" class="checker" />third
<input type="radio" name="cureway" id="forth" class="checker" />forth
<input type="radio" name="cureway" id="five" class="checker" />five
关于javascript - 单击一个单选按钮并检测其他未选中的 radio ,在我的 jquery 情况下该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30388484/