How to change radio button to 'checked' when another is selected?

萝らか妹 提交于 2021-02-05 09:33:35

问题


I hope you guys can help me with this one.

How can I have a radio button id="A" change it's attribute to 'checked' when radio button id="B" is selected?

Radio buttons A and B are in different groups.

Thanks!


回答1:


<input type="radio" name="group1" id="A">Radio A
<input type="radio" name="group2" id="B">Radio B
<input type="radio" name="group2" id="C">Radio C

$("input[type='radio'][name='group2']").change(function() {
  if ($('#B').is(':checked')) {
    $('#A').prop("checked", true);
  } else {
    $('#A').prop("checked", false);
  }
});

Here is the jsFiddle http://jsfiddle.net/8kdb7d6o/




回答2:


$("#B").change(function(){
    $("#A").prop('checked', true);
});



回答3:


to check if radio input checked or not

if($('#B').is('checked')){
   $("#A").prop('checked', true);
}


来源:https://stackoverflow.com/questions/34186848/how-to-change-radio-button-to-checked-when-another-is-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!