简体   繁体   English

使用jquery获取已检查单选按钮的值

[英]get value of checked radio button with jquery

I want to get the value of selected radio field on change. 我希望在更改时获得所选无线电字段的值。 If the selected value is Admin I want to disable the group of fields with ID permissions_forms and all those checkboxes inside it should be checked. 如果所选值是Admin我想要禁用具有ID permissions_forms的字段组,并且应该检查其中的所有复选框。 Otherwise,if the value is not Admin they should be enabled. 否则,如果值不是Admin ,则应启用它们。

I tried to get the value but the function of change is not working in my case. 我试图获得价值,但改变的功能在我的情况下不起作用。 Can somebody help me, please? 请问有人可以帮助我吗? Here is Jsfiddle 这是Jsfiddle

Here is HTML code: 这是HTML代码:

<ul class="radio" id="role">
  <li>
    <input id="role-0" name="role" type="radio" value="User">
    <label for="role-0">User</label>
  </li>
  <li>
    <input checked="" id="role-1" name="role" type="radio" value="Admin">
    <label for="role-1">Admin</label>
  </li>
</ul>
<br/><br/><br/><br/>
<ul class="checkbox" id="permissions_forms">
  <li>
    <input checked="" id="diagnsosis_forms-0" name="diagnsosis_forms" type="checkbox">
    <label for="diagnsosis_forms-0"> Checkbox enable when admin selected</label>
  </li>
  <li>
    <input checked="" id="diagnsosis_forms-1" name="diagnsosis_forms" type="checkbox" >
    <label for="diagnsosis_forms-1"> Checkbox enable when admin selected</label>
  </li>
</ul>

And the jQuery code: 和jQuery代码:

$('#role').change(function() {
  selected = $('input[name=role]:checked').val();
  if (selected == "Admin") {
    alert('Admin');
    $("#permissions_forms :input").attr("disabled", true);
    $("#permissions_forms").parent().siblings().find(':checkbox').attr('checked', this.checked);
  }
  else{
  $("#permissions_forms :input").attr("disabled", false);
  }
});

Class or Id? 班级还是Id?

Do not assign the same id to multiple elements. 不要为多个元素分配相同的ID。 Id should be unique. Id应该是唯一的。 Use class instead. 请改用类。

<div id="im-unique">Earth</div>

<div class="im-not-unique">Star 1</div>
<div class="im-not-unique">Star 2</div>

How to toggle the properties disabled and checked with jquery? 如何使用jquery切换disabledchecked的属性?

If the selected value is Admin I want to disable the group of fields with ID permissions_forms and all those checkboxes inside it should be checked. 如果所选值是Admin,我想要禁用具有ID permissions_forms的字段组,并且应该检查其中的所有复选框。

$checkboxes.prop({ "disabled": true, 'checked': true}); will make the checkboxes disabled and checked. 将禁用和选中复选框。

Why prop not attr ? 为什么propattr

from jquery's doc: 来自jquery的文档:

To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method. 要检索和更改DOM属性(如表单元素的已检查,已选择或已禁用状态),请使用.prop()方法。

 function onChange() { var selected = $('input[name=role]:checked').val(); var $checkboxes = $("#diagnsosis_forms-0, #diagnsosis_forms-1", "#permissions_forms"); if (selected == "Admin") { $checkboxes.prop({ "disabled": true, 'checked': true}); } else { $checkboxes.prop("disabled", false); } } onChange() $('#role').change(onChange); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="radio" id="role"> <li> <input id="role-0" name="role" type="radio" value="User" class=""> <label for="role-0">User</label> </li> <li> <input checked="" id="role-1" name="role" type="radio" value="Admin"> <label for="role-1">Admin</label> </li> </ul> <br/><br/><br/><br/> <ul class="checkbox" id="permissions_forms"> <li> <input checked="" id="diagnsosis_forms-0" name="diagnsosis_forms" type="checkbox"> <label for="diagnsosis_forms-0"> Checkbox enable when admin selected</label> </li> <li> <input checked="" id="diagnsosis_forms-1" name="diagnsosis_forms" type="checkbox" > <label for="diagnsosis_forms-1"> Checkbox enable when admin selected</label> </li> </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM