jquery - 如何基于另一个单击按钮设置一个单选按钮 [英] jquery -- how to set one radio button based on another getting clicked

查看:79
本文介绍了jquery - 如何基于另一个单击按钮设置一个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含以下内容的表单:

Let's say I have a form that includes this:

<input type='radio' name='o37_field1' value='aaa'>aaa
<input type='radio' name='o37_field1' value='bbb'>bbb

<input type='radio' name='o44_field1' value='aaa'>abcdef
<input type='radio' name='o44_field1' value='bbb'>baksdf

<input type='radio' name='o58_field1' value='aaa'>wisdof
<input type='radio' name='o58_field1' value='bbb'>safhwr

这是我需要用jquery做的但遇到麻烦:

Here's what I need to do with jquery but am having trouble:

如果 aaa在其中一个集合中单击,也为其他集合设置/检查aaa,并对其他任何具有field1作为名称和aaa作为值的单选按钮执行相同操作。

If "aaa" is clicked on with in one of the sets, also set/check "aaa" for the other ones, and do the same for any other radio buttons that have "field1" as part of the name and "aaa" as the value.

同上bbb

任何想法?

推荐答案

这将设置所有ra选中一个时,值为aaa的dios:

This will set all radios with a value of "aaa" when one is checked:

$(document).ready(function() {
   $(':radio[value=aaa]').change(function(){
       $(':radio[value=aaa]').prop('checked',true);
   });
});

更强大的解决方案,例如在点击一个时选择所有喜欢值:

The more robust solution like selecting all "like" values when one is clicked:

$(document).ready(function() {
    $(':radio').change(function(){       
       $(':radio[name$='field1'][value='+$(this).val()+']').prop('checked',true);
   });
});

这篇关于jquery - 如何基于另一个单击按钮设置一个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆