【发布时间】:2011-03-16 16:39:55
【问题描述】:
这是我的设置:如果在页面加载时已经检查了radiobutton1(它在查看源中有checked="checked")并且我在选择radiobutton2 后尝试取消选中它,checked 上的checked 属性radiobutton1选择radiobutton2后没有被删除:
if($('#' + radiobutton1_ID).is(':checked'))
UncheckRadioButton('#' + radiobutton2_ID);
// Utility function to clear a radiobutton's "checked" attribute
function UncheckRadioButton(radiobuttonID) {
radiobuttonID.removeAttr("checked");
}
选择radiobutton2 并执行“查看源代码”后,我在radiobutton2 上看不到checked="checked"(即使页面显示此按钮已选中),对于radiobutton1 它仍然显示checked="checked"。这是为什么?应该是相反的。
更新
这是我的更多代码。我知道if 语句部分受到打击,所以这不是问题,而且我知道我使用的单选按钮 ID(ccRadioBtn、checkRadioBtn 和 paypalRadioButton)是正确的:
var ccRadioBtn = ccRadioButton; // credit card radio button ID
var paypalRadioButton = payPalRadioClientID;
// ...
if (paypalIsSelected()) {
UncheckRadioButton(checkRadioBtn);
UncheckRadioButton(ccRadioBtn);
// ...
} else {
// force a clear on any previously selected payment options
CheckRadioButton(ccRadioBtn); // default to credit card radio button
UncheckRadioButton(paypalRadioButton);
UncheckRadioButton(checkRadioBtn);
// ...
}
}
function UncheckRadioButton(radiobuttonID) {
$('#' + radiobuttonID).removeAttr("checked");
}
function CheckRadioButton(radiobuttonID) {
$('#' + radiobuttonID).attr('checked', true);
}
问题是:如果一个单选按钮默认为checked="checked" 并且我单击另一个单选按钮,则应该删除第一个单选按钮的checked 属性,但事实并非如此。并且已选择的第二个单选按钮没有应有的checked="checked"。当我查看页面源时,我可以看到这一点。我不确定为什么这不起作用。
【问题讨论】:
-
我已经用我的实际代码更新了我的帖子。问题依然存在。
-
问题最终不是 JS 问题,但我确实从这个线程中得到了很多。感谢所有回复的人。
标签: jquery radio-button checked