单选按钮检查属性

在我的页面中,我有三个单选按钮,我想在单击

  • 时检查单选按钮。 我的点击function正在运行,但点击时未检查收音机。 我怎样才能做到这一点? 我的代码如下

     $(document).ready(function() { $('#myContainer li').click(function() { $('#radio1').attr('checked'); }); }); 
  • val1
  • val2
  • val3
  • 您正在获取已检查的属性而不是设置它。 试试这个:

     $('#radio1').attr('checked', 'checked'); 

    注意

    从jQuery 1.6开始,建议使用.prop()而不是.attr()

     $("#radio1").prop("checked", true); 
     $('#radio1').attr('checked')' 

    将返回值,以便设置值try

     $('#myContainer li').click(function() { $('#radio1').attr('checked','checked'); }); 

    或者如果你使用jquery 1.6+使用prop

     $('#myContainer li').click(function() { $('#radio1').prop('checked',true); }); 

    DMEO

    在你的情况下,无论你点击哪一个,它都会用id = #radio1检查收音机


    在我看来,更合适的行为是

     $('#myContainer li').click(function() { $(":input",this).prop('checked',true); }); 

    DEMO