2013-05-28 66 views
0

我想切換單元格表中包含的單選框選擇。切換單選按鈕只能工作一次(刷新)?

切換每個單選按鈕只能工作一次。

之後,所有的單選按鈕被禁用,但我的HTML代碼似乎是正確的;屬性checked="checked"存在。

爲什麼這不起作用?

這裏是jQuery的我使用:

$('td').click(function(){ 
    var $elem = $(this).children('input');    
    var name = $elem.attr('name'); 
    $('input[name='+name+']').each(function(){ 
     $(this).removeAttr('checked');     
    }).promise().done(function(){     
     $elem.attr('checked','checked');    
    }); 
}); 

而且圖像:

enter image description here

回答

5

您需要使用prop()你目前正實際刪除使用removeAttr()屬性,而不是隻是將其設置爲禁用。

下面的工作:

$('input[name='+name+']').each(function(){ 
    $(this).prop('checked', false);     
}).promise().done(function(){     
    $elem.prop('checked', true);    
}); 
+0

'prop'做的伎倆,但我不明白爲什麼'attr'不refrech的UI ?? !! – sdespont

+0

我不認爲'removeAttr'是原因。實際上,'$(this).attr('checked',false);'不起作用。我需要使用'prop'。感謝您的快速幫助。 – sdespont

+1

@sdespont我不認爲'attr('checked',false)'存在,使用attr設置爲false的方法是使用'removeAttr',所以問題是removeAttr我相信:) – lifetimes