2

这是我的设置:如果radiobutton1在页面加载时已经选中(它checked="checked"在查看源代码中)并且我在选择后尝试取消选中它,则在选择后radiobutton2属性checkedonradiobutton1不会被删除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并执行“查看源代码”后,我看不到任何checked="checked"内容radiobutton2即使页面显示此按钮已选中),并且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"应有的值。当我查看页面源时,我可以看到这一点。我不确定为什么这不起作用。

4

1 回答 1

5

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

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 将显示页面的原始源,而不是您当前正在查看的状态。

于 2010-07-15T22:36:27.697 回答