如何处理jQuery UI Selectmenu更改事件

时间:2014-07-06 13:36:59

标签: jquery jquery-ui select-menu

我使用带有custom render option

的jquery ui选择菜单

我如何处理change事件?

我试试

   $('#filesA').on('change', function() {
  alert( 'x'); 
});

但它不能与jQuery UI Selectmenu一起使用

我也尝试

$( "#filesA" ).selectmenu({
  change: function( event, ui ) {}
});

它正在工作,但它会创建另一个选择菜单实例!!

enter image description here

我的js代码

$( document ).ready(function() {

  $( "#filesA" ).selectmenu({ change: function( event, ui ) { alert('x'); }});



$.widget( "custom.iconselectmenu", $.ui.selectmenu, {
                            _renderItem: function( ul, item ) {
                                var li = $( "<li>", { text: item.label } );

                                if ( item.disabled ) {
                                    li.addClass( "ui-state-disabled" );
                                }

                                $( "<span>", {
                                    style: item.element.attr( "data-style" ),
                                    "class": "ui-icon " + item.element.attr( "data-class" )
                                })
                                .appendTo( li );

                                return li.appendTo( ul );
                            }
                        });

                        $( "#filesA" )
                        .iconselectmenu()
                        .iconselectmenu( "menuWidget" )
                        .addClass( "ui-menu-icons" );




});

和我的HTML代码

                <label class="langLabel" for="filesA">Select your language:</label>
                <select name="filesA" id="filesA">
                    <option value="lan1">Test Lang1</option>
                    <option value="lan2">Test Lang2</option>
                    <option value="lan3">Test Lang3</option>
                    <option value="lan4">Test Lang4</option>
                    <option value="lan5">Test Lang5</option>
                 </select>

5 个答案:

答案 0 :(得分:42)

只需将触发器'change'更改为'selectmenuchange'

即可
$('#filesA').on('selectmenuchange', function() {
    alert( 'x'); 
});

答案 1 :(得分:7)

看看这里:http://jsfiddle.net/JLVSM/

只需将您的代码更改为:

$( "#filesA" ).selectmenu({ change: function( event, ui ) { alert('x'); }});

$.widget( "custom.iconselectmenu", $.ui.selectmenu, {
    _renderItem: function( ul, item ) {
        var li = $( "<li>", { text: item.label } );

        if ( item.disabled ) {
            li.addClass( "ui-state-disabled" );
        }

        $( "<span>", {
            style: item.element.attr( "data-style" ),
            "class": "ui-icon " + item.element.attr( "data-class" )
        })
        .appendTo( li );

        return li.appendTo( ul );
    },
});

$( "#filesA" ).addClass( "ui-menu-icons" );

答案 2 :(得分:3)

我遇到了同样的问题。最终用iconselectmenu而不是selectmenu克服了它

$( "#filesA" ).iconselectmenu({ change: function( event, ui ) { alert('x'); }});

答案 3 :(得分:3)

或更具体地......

$(function() {
$.widget( "custom.iconselectmenu", $.ui.selectmenu, {
_renderItem: function( ul, item ) {
var li = $( "<li>", { text: item.label } );
if ( item.disabled ) {
li.addClass( "ui-state-disabled" );
}
$( "<span>", {
style: item.element.attr( "data-style" ),
"class": "ui-icon " + item.element.attr( "data-class" )
})
.appendTo( li );
return li.appendTo( ul );
}
});

$( "#filesB" )
.iconselectmenu()
.iconselectmenu( "menuWidget" )
.addClass( "ui-menu-icons customicons" );

$('#filesB').iconselectmenu({
    change: function( event, ui) {
    alert('something has changed');
    }
});
});

答案 4 :(得分:3)

我这样解决了:

exec("/bin/myScript.pl $criteria1", $outputArray);

foreach ($outputArray as $item) { // <------
    print $item . "<br />";
}