jQuery buttonset刷新不工作,因为我'

jQuery buttonset refresh not working as I'd expected

本文关键字:因为 buttonset 刷新 工作 jQuery      更新时间:2023-09-26

OK -我有一个函数,我调用它来动态添加一个单选按钮作为这个问题。这是完整的功能:

    // 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对应文本的新标签。名称(在给定的示例中)。

我也注意到在调用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,让您看到一个工作的例子。

一定是bug。将jQuery版本从1.7.1更改为1.8.3,在jsfiddle1中选择UI,您将看到它现在按预期工作。

code here