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

Pos*_*Guy 2 jquery radio-button checked

这是我的设置:如果radiobutton1在页面加载时已经检查过(它checked="checked"在视图源中)并且我在选择后尝试取消选中,则在选择后radiobutton2,checked属性on 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"); 
}
Run Code Online (Sandbox Code Playgroud)

选择后radiobutton2,做一个"查看源文件"我没有看到checked="checked"radiobutton2(即使页会显示该按钮选中),并为radiobutton1它仍然显示checked="checked".这是为什么?它应该是相反的方式.


UPDATE

这是我的一些代码.我知道if声明部分被击中,所以这不是问题,我知道我使用的单选按钮ID(ccRadioBtn,checkRadioBtnpaypalRadioButton)是正确的:

    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);
}
Run Code Online (Sandbox Code Playgroud)

问题是:如果一个单选按钮默认为checked="checked"并且我单击另一个单选按钮,checked则应删除第一个单选按钮的属性,但事实并非如此.并且已经选择的第二个单选按钮没有checked="checked"应有的功能.当我查看页面源时,我可以看到这一点.我不确定为什么这不起作用.

Nic*_*ver 5

你在这里传递一个字符串:

UncheckRadioButton('#' + radiobutton_2_ID);
Run Code Online (Sandbox Code Playgroud)

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

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

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

$(radiobuttonID).removeAttr("checked");
Run Code Online (Sandbox Code Playgroud)

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