gpt4 book ai didi

jquery - 选择另一个单选按钮后无法取消选中

转载 作者:行者123 更新时间:2023-12-01 03:04:49 24 4
gpt4 key购买 nike

这是我的设置:如果在页面加载时已经选中 radiobutton1 (在查看源代码中有 checked="checked"),并且我尝试在之后取消选中它选择radiobutton2,选择radiobutton2后,radiobutton1上的checked属性不会被删除:

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(ccRadioBtncheckRadioBtnpaypalRadioButton)我使用的是正确的:

    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" 。当我查看页面源代码时我可以看到这一点。我不确定为什么这不起作用。

最佳答案

您在此处传递一个字符串:

UncheckRadioButton('#' + radiobutton_2_ID);

但尝试在这里将其用作 jQuery 对象:

radiobuttonID.removeAttr("checked");
//which is actually doing this:
"#something".removeAttr("checked"); //.removeAttr() is not a function for string

您需要包装它才能将其用作选择器,如下所示:

$(radiobuttonID).removeAttr("checked");

至于查看源代码...这取决于浏览器,例如 IE 将显示页面的原始源代码,而不是您当前正在查看的状态。

关于jquery - 选择另一个单选按钮后无法取消选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3260625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com