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

发布于 2024-12-18 02:22:20 字数 2030 浏览 1 评论 0原文

好的 - 我有一个函数,我调用它来动态添加单选按钮 根据这个问题。这是完整的函数:

    // 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

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

Here's a screenshot of what happens

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∝单色的世界 2024-12-25 02:22:20

我的错误。实际上,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 供您查看工作示例。

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:

<label><input /></label>

And the plugin expects:

<input /><label for=''></label>

Here is the modified function:

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

Don't forget to initiliaze the plugin, even if the container '#radioX' is empty:

$('#radioX').buttonset();

I have made a jsfiddle for you to see a working example.

傲影 2024-12-25 02:22:20

这一定是一个错误。将 jQuery 版本从 1.7.1 更改为 1.8.3,在 jsfiddle1 中选择 UI,你会看到它现在正在按预期工作。

code here

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.

code here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文