2010-07-15 50 views
2

这里后,取消一个单选按钮是我的设置:如果radiobutton1已经选中加载页面时(它有checked="checked"在查看源代码),我尝试选择radiobutton2后取消它,在radiobutton1checked属性选择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"。这是为什么?它应该是相反的。


UPDATE

下面是一些我的代码。我知道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"。当我查看页面源时,我可以看到这一点。我不知道为什么这不起作用。

+0

我用我的实际代码更新了我的帖子。问题依然存在。 – PositiveGuy 2010-07-16 02:12:03

+0

这个问题最终不是一个JS问题,但我从这个线程中获得了很多。感谢所有回复的人。 – PositiveGuy 2010-07-16 03:51:22

回答

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"); 

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

+0

感谢这是一个漫长的一天,我没有意识到这一点。 – PositiveGuy 2010-07-16 01:51:10

+0

它仍然无法正常工作。我不知道为什么,但如果我再次选择radiobutton1,按照预期查看源代码checked =“checked”。但是,当我改变并选择radiobutton2然后查看源代码后,radiobutton1仍然检查=“checked”,而radiobutton2根本没有checked属性。这里有什么我不知道的DOM吗?也许查看源代码不代表当前状态?这意味着如果我在radiobutton1后更改为radiobutton2,直到回传才能实现状态? – PositiveGuy 2010-07-16 02:05:31

+0

@coffeeaddict - 用户界面是否被检查,而不是查看源?你也使用哪个浏览器?您应该使用Chrome或Firebug并检查此元素以获取准确*当前*视图。 – 2010-07-16 02:16:19