简体   繁体   中英

jQuery UI Selectmenu events bind

Im using JQuery UI Selectmenu widget - http://wiki.jqueryui.com/w/page/12138056/Selectmenu

Im trying to bind change event. But it does not work:

$(function() {
  $('select#items').selectmenu();
  $('select#items').bind("change",function(){
     alert('x');
   });  
});

Any ideas ?

I found an answer. So here it is:

$(function() {      
        $('#items').selectmenu({
            change: function() {
                alert('x');
            }
        });
});

Since this came up first on Google and didn't give me the answer I was looking for, having looked at the jquery ui code it can be done after initially setting up the select menu, binding to the selectmenuchange event as below. Worked for me anyway.

$('#items').selectmenu();

$('#items').on('selectmenuchange',function() {

    alert("hello world");

});

I was just pulling my hair out about this and figured out a easy way of doing it. You basically have to tell the selectmenu to trigger the change event.

When you setup your selectmenu use the close method like so:

//Set select box to jQuery UI selectmenu
$('select').selectmenu({
   close: function(event, ui){
      //Fire change event
      $(this).change();
   }
});

Then define your change event like this:

$('#container').on('change', 'select', function(e){
   alert('x');
});

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