2013-05-10 34 views
1

假設爲checkdisable選中複選框並在另一個框上反轉checkdisable。似乎工作,直到我再次嘗試在以前更改複選框。大腦今天被炸了。將非常感謝任何見解或更好的代碼。Jquery Swap選中禁用attr更改

http://jsfiddle.net/sz7R4/

$('.changeCheck').change(function(event) { 
    var oldTarget = $(':checked').attr('id'); 
    console.log(oldTarget); 
    $(this).attr('disabled', 'disabled').attr('checked', 'checked'); 
    $('#'+oldTarget).removeAttr('checked', 'checked').removeAttr('disabled', 'disabled'); 
}); 

回答

3

當前您只針對第二個輸入,您還需要針對第一個輸入中的change事件。除了你應該使用prop()而不是attr()

$('.changeCheck, #check1').change(function(event) { 
    $(this).prop("disabled", true).siblings().prop("disabled", false).prop('checked', false); 
}); 

Updated Fiddle

+0

我能夠使用你的榜樣和擴大它。我決定最好在所有複選框上都有'.changeCheck',這樣兄弟姐妹才能更好地工作。感謝所有人的幫助! – sin0cide 2013-05-10 22:48:50

0

.attr()只改變默認的屬性,它不會改變 「住」 屬性(如jQuery的1.6)。您需要改爲使用.prop()方法。

2

試試這個:

$('.changeCheck').change(function (event) { 
    $('.changeCheck').prop({ 
     'checked': false, 
     'disabled': false 
    }); 
    $(this).prop({ 
     'checked': true, 
     'disabled': true 
    }); 
}); 

jsFiddle example

你不想使用.removeAttr().attr(),而是.prop()。在這裏不會有單選按鈕是更好的選擇嗎?

+0

+1的單選按鈕 – daniel 2013-05-10 03:46:14