简体   繁体   中英

How to dynamically add button to jQuery-UI button-set?

I am looking for a way to add a button to a jQuery button-set at run-time with JavaScript.

        if(response.success) {
            var menuItem = document.createElement('a');

            $(menuItem).attr('href', response.href);
            $(menuItem).attr('category-id', response.id);
            $(menuItem).text(title);

            $("#menu-buttonset").append(menuItem);
        }

I am adding the hyperlink to the div container of the button-set using JavaScript, and then re-calling buttonset() on the container.

 $("#menu-buttonset").buttonset();

This however does not seem to rebuild the button-set as I expected.

I am not seeing any function in the jQuery API for this either.

Can anyone provide some insight?

Resolved:

Using refresh method as suggested, rebuilt the button-set.

$('#menu-buttonset').buttonset('refresh');

It seems to me like buttonsets use radiobuttons or checkboxes, not links:

http://docs.jquery.com/UI/Button

Take a look here: http://jsfiddle.net/Fe9Tx/1/ (Fixed it so selection works)

You need to call the refresh method after adding an new item to the button set.

$("#menu-buttonset").append(menuItem).buttonset('refresh');

I cannot find any documentation for this method in jquery, but it is available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM