javascript - 单击一个单选按钮并检测其他未选中的 radio ,在我的 jquery 情况下该怎么做?

标签 javascript jquery

我想在用户单击第一个单选按钮时执行一个操作,它还会检测其他四个未选中的单选按钮。我面临一个挑战,我不能一个一个地使用 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/

相关文章:

javascript - 在 OS X 10.8.2 上构建 ember.js 失败

javascript - 为什么库上设置的 "developmentMode"标志被忽略?

jQuery 用图像填充正文

jquery - 使用 cherrio 选择器获取评论元素文本

javascript - 如何使用angular js在母版页中设置事件菜单

javascript - 在 vuejs 中将旧代码修改为 vue router 4

javascript - lodash _.size() 比 JS length 属性更快吗?

javascript - 你能帮我处理剪刀石头布游戏 javascript 和一些 HTML 吗?

javascript - 使用 javascript 或 jquery 移调音乐

ajax - 如果函数已经存在,则防止重新加载 javascript。否则确保同步加载