好的 - 我有一个函数,我调用它来动态添加单选按钮 as per this question 。这是完整的功能:
// function used to create a new radio button
function createNewRadioButton(
selector,
newRadioBtnId,
newRadioBtnName,
newRadioBtnText){
// create a new radio button using supplied parameters
var newRadioBtn = $('<input />').attr({
type: "radio", id: newRadioBtnId, name: newRadioBtnName
});
// create a new label and append the new radio button
var newLabel = $('<label />').append(newRadioBtn).append(newRadioBtnText);
// add the new radio button and refresh the buttonset
$(selector).append(newLabel).append('<br />').buttonset("refresh");
}
因此,如果我使用以下代码调用上述函数,我希望在 div '#radioX' 中已包含的单选按钮下方添加另一个单选按钮(假设有一个 id radioX 的 div 包含单选按钮) ):
// create a new radio button using the info returned from server
createNewRadioButton(
'#radioX', // Radio button div id
product.Id, // Id
product.Id, // Name
product.Name // Label Text
);
鉴于文档准备就绪,我告诉 jQuery 从 #radioX 中包含的单选按钮创建一个按钮集,如下所示:$( "#radioX" ).buttonset();
,为什么不调用 $("#radioX").buttonset("refresh")
在函数 createNewRadioButton 中刷新单选按钮列表?
调用 createNewRadioButton 后看到的结果是添加了一个新标签,其中添加了所需的文本,但没有新的单选按钮。因此,我看到的不是一个漂亮的新 jQuery 单选按钮,而是一个新标签,其中的文本与 Product.Name 等效(在给定的示例中)。
我还注意到在调用 createNewRadioButton 后 Firebug 中出现此警告输出 - 这可能与此有关吗?
reference to undefined property $(this).button("widget")[0]
编辑
最佳答案
我的错误。实际上,refresh
方法在运行时很好地处理了添加的单选元素。
我认为您在 createNewRadioButton()
中生成的标记与插件期望的不兼容。
您创建:
<label><input /></label>
插件期望:
<input /><label for=''></label>
这是修改后的函数:
function createNewRadioButton(
selector,
newRadioBtnId,
newRadioBtnName,
newRadioBtnText){
// create a new radio button using supplied parameters
var newRadioBtn = $('<input />').attr({
type: "radio", id: newRadioBtnId, name: newRadioBtnName
});
// create a new label and append the new radio button
var newLabel = $('<label />').attr('for', newRadioBtnId).text(newRadioBtnText);
// add the input then the label (and forget about the <br/>
$(selector).append(newRadioBtn).append(newLabel).buttonset("refresh");
}
即使容器“#radioX”为空,也不要忘记初始化插件:
$('#radioX').buttonset();
我做了一个jsfiddle让您查看一个工作示例。
关于javascript - jQuery 按钮集刷新无法按我的预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8289961/