jQuery 按钮集刷新无法按我的预期工作
好的 - 我有一个函数,我调用它来动态添加单选按钮 根据这个问题。这是完整的函数:
// 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();
,为什么不在函数 createNewRadioButton 中调用 $("#radioX").buttonset("refresh")
刷新单选按钮列表?
调用 createNewRadioButton 后看到的结果是添加了一个新标签,其中添加了所需的文本,但没有新的单选按钮。因此,我看到的不是一个漂亮的新 jQuery 单选按钮,而是一个新标签,其中的文本与 Product.Name 等效(在给定的示例中)。
我还注意到在调用 createNewRadioButton 后 Firebug 中出现此警告输出 - 这可能与此有关吗?
reference to undefined property $(this).button("widget")[0]
编辑
OK - I have a function which I call to dynamically add a radio button as per this question. This is the full function:
// 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");
}
So if I were to call the above function with the following code I would expect another radio button to be added underneath the radio buttons already contained in the div '#radioX' (assuming there is a div with id radioX containing radio buttons):
// 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
);
Given that on document ready, I tell jQuery to create me a buttonset out of the radio buttons contained in #radioX like so: $( "#radioX" ).buttonset();
, why doesn't the call to $("#radioX").buttonset("refresh")
in the function createNewRadioButton refresh the list of radio buttons?
The result that I see after a call to createNewRadioButton is made is a new label with the desired text is added but no new radio button. So instead of a nice new jQuery radio button sitting underneath whatever was already there, I see just a new label with text equivelant to product.Name (in the given example).
I've also noticed this warning output in firebug after a call is made to createNewRadioButton - could this have anything to do with it?
reference to undefined property $(this).button("widget")[0]
EDIT
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的错误。实际上,
refresh
方法在运行时很好地处理了添加的单选元素。我认为您在 createNewRadioButton() 中生成的标记与插件期望的不兼容。
您创建:
并且插件期望:
这是修改后的函数:
即使容器“#radioX”为空,也不要忘记初始化插件:
我制作了一个 jsfiddle 供您查看工作示例。
My mistake. Actually the
refresh
method is taking good care of added radio elements at runtime.The markup you are generating in
createNewRadioButton()
is I think not compatible with what the plugin expects.You create:
And the plugin expects:
Here is the modified function:
Don't forget to initiliaze the plugin, even if the container '#radioX' is empty:
I have made a jsfiddle for you to see a working example.
这一定是一个错误。将 jQuery 版本从 1.7.1 更改为 1.8.3,在 jsfiddle1 中选择 UI,你会看到它现在正在按预期工作。
It must be a bug. Change jQuery version from 1.7.1 to 1.8.3, choose UI in jsfiddle1 and you will see that it is now working as expected.