2011-03-17 141 views
0

我目前在一些单选按钮上使用jQuery。当用户点击收音机时,会调用一个功能,从所有其他收音机中删除checked属性,并禁用与其他收音机相关的输入字段。例如,当一个无线点击,jQuery单选按钮选择/取消选择

function EnableRadio1() { 
    $('#x :input').attr('checked', false).attr('disabled', true); 
    $('#y :input').attr('checked', false).attr('disabled', true); 
    $('#z :input').attr('checked', false).attr('disabled', true); 
    $('#n :input').attr('checked', false).attr('disabled', true); 

    //enable segs_by_all options 
    $('#radio1 :input').removeAttr('disabled'); 
    } 

是由我的Rails助手叫, <%= radio_button_tag 'users', 'all', false, :id => 'radio1', :onclick => 'EnableRadio1()' %>
什么是更简洁,jQueryesque方式做到这一点?非常感谢。

回答

2

如果我理解你...

$(".radio").change(function(){ 
    $(":radio").not(this).attr({"checked": false, "disabled": true}); 
    $(this).removeAttr("disabled"); 
});