23

我使用带有自定义渲染选项的 jquery ui 选择菜单

我该如何处理这个change事件?

我试试

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

但它不适用于 jQuery UI Selectmenu

我也试试

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

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

在此处输入图像描述

我的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>
4

5 回答 5

52

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

$('#filesA').on('selectmenuchange', function() {
    alert( 'x'); 
});
于 2015-02-26T11:03:30.153 回答
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" );
于 2014-07-06T14:20:30.207 回答
3

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

$( "#filesA" ).iconselectmenu({ change: function( event, ui ) { alert('x'); }});
于 2014-12-16T21:57:36.860 回答
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');
    }
});
});
于 2014-12-16T23:11:43.153 回答
3

我这样解决了:

$.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({ change: function( event, ui ) { alert("Hi"); s}}).iconselectmenu( "menuWidget" ).addClass( "ui-menu-icons" );
于 2016-11-21T20:16:35.330 回答