如何从组中按名称获取布尔值(至少1个选中的单选按钮)(jquery)

时间:2019-01-19 11:12:29

标签: jquery

如何使用jQuery从属性名称中获取单选按钮组的布尔值而不是实际选择的文本

1 个答案:

答案 0 :(得分:0)

$(“ input [name ='MainColumn']”)。is(':checked')

console.log($("input[name='MainColumn']").is(':checked'));//true or false 
console.log($("input:radio[name=MainColumn]:checked").length);//0 or 1

$("input").on('click',function(){
console.log($("input[name='MainColumn']").is(':checked'));//true or false 
console.log($("input:radio[name=MainColumn]:checked").length)//0 or 1

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" name="MainColumn" value="male"> Male<br>
  <input type="radio" name="MainColumn" value="female"> Female<br>
  <input type="radio" name="MainColumn" value="other"> Other