我想要做的是讓用戶單擊一個表,選擇該表內的單選按鈕,然後向所選表中添加一個類。然後,如果用戶在選中該無線電時選擇了另一個表,則該類將被添加,但是之前選擇的表/無線電的類也需要被刪除。無法刪除先前選定的單選按鈕的類
現在大部分工作,接受當我選擇另一個電臺時,以前的類不會被刪除。只有當你切換收音機時,它纔會被刪除。
Here's a link to a demo on jsfidd在哪裏可以看到所有的html和其他JS。
$(function() {
$('table').click(function(event) {
if (event.target.type != "radio") {
var that = $(this).find('input:radio');
that.attr('checked', !that.is(':checked'));
if (that.is(':checked')) {
that.closest('table').addClass('selected');
}
else {
that.closest('table').removeClass('selected');
}
}
});
});
我也忘了我在使用另一個版本之前,我修改了它。在這一個中,如果你真的點擊單選按鈕,樣式將正確添加/刪除,但是當你點擊表格時將不會。我試圖結合這兩個,那就是我如何得到我張貼上面的功能..
This is the link to the jsfiddle with that demo這裏是我也使用jQuery。
// jQuery to select radio button within clicked table
$(function() {
$('table').click(function(event) {
if(event.target.type != "radio") {
var that = $(this).find('input:radio');
that.attr('checked', !that.is(':checked'));
}
});
});
// jQuery to change the style of the table containing the selected radio button
$(function() {
$('input[type="radio"]').change(function() {
// grab all the radio buttons with the same name as the one just changed
var this_radio_set = $('input[name="'+$(this).attr("name")+'"]');
for (var i=0; i < this_radio_set.length;i++) {
if ($(this_radio_set[i]).is(':checked'))
$(this_radio_set[i]).closest('table').addClass('selected');
else
$(this_radio_set[i]).closest('table').removeClass('selected');
}
});
});
謝謝你的快速反應。我會在我的if(that.is(':checked')){或代替嗎?我不太明白。 – xxstevenxo
看看我把它放在你的代碼中(在演示鏈接中)。 – Blender
非常感謝!完美的工作! :DD – xxstevenxo