javascript - jQuery 按钮集刷新无法按我的预期工作

标签 javascript jquery jquery-ui refresh jquery-ui-button

好的 - 我有一个函数,我调用它来动态添加单选按钮 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]

编辑

Here's a screenshot of what I expected to happen:

Here's a screenshot of what happens

最佳答案

我的错误。实际上,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/

相关文章:

javascript - 拖放和缩放图像,但用户上传的图片不可拖动和调整大小

javascript - 从同一类中的另一个方法访问一个方法,该方法作为 props 传递给另一个类

javascript - HTML 以纯文本形式显示

javascript - Angular 与数据表注入(inject)器错误

javascript - 滚动到特定高度后删除类 jQuery

jQueryUI 选项卡将鼠标悬停在禁用的链接上,如何更改光标使其看起来不可点击?

javascript - 当悬停在覆盖第一个的另一个元素上时,停止触发悬停的 'out' 事件

Jquery datepicker 克隆问题

javascript - 单击时在特定点导航到不同页面

jquery - Rails 3.1,jQuery UI 不加载