javascript 触发 onClick 事件 jQuery Mobile 选择菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8525017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:43:35  来源:igfitidea点击:

Fire onClick event jQuery Mobile Select Menu

javascriptjquery-mobile

提问by zareef

jQuery Mobile gives us these nice custom select menus where the menu comes in as an overlay. I'm trying to attach an onclick function to these options but since jQuery mobile replaces the option tags with their own generated tags. I can't seem to get the function to attach to the "options" (which are actually generated as styled links).

jQuery Mobile 为我们提供了这些漂亮的自定义选择菜单,其中菜单作为叠加层出现。我试图将 onclick 函数附加到这些选项,但由于 jQuery mobile 用自己生成的标签替换了选项标签。我似乎无法将函数附加到“选项”(实际上是作为样式链接生成的)。

回答by Jasper

Rather than binding to the clickevent for the "fake-option" elements, how about binding to the changeevent for the <select>element:

与其绑定到click“fake-option”元素的事件,不如绑定到元素的change事件<select>

$('#the-select').on('change', function () {
    var $this = $(this),
        val   = $this.val();
});

Here is a demo: http://jsfiddle.net/PQ39n/

这是一个演示:http: //jsfiddle.net/PQ39n/

Note that .on()is new in jQuery 1.7 and in this case is the same as .bind().

请注意,这.on()是 jQuery 1.7 中的新内容,在这种情况下与.bind().

EDIT

编辑

If you do want to bind to the clickevent for the "fake-option" elements:

如果您确实想绑定到click“假选项”元素的事件:

$('#the-page').on('click', '.ui-selectmenu-list > li', function () {
    alert('onClick = ' + $('#the-select').children().eq($(this).attr('data-option-index')).val());
});

Here is a demo: http://jsfiddle.net/PQ39n/(same demo as above)

这是一个演示:http: //jsfiddle.net/PQ39n/(与上面的演示相同)

In this example .on()is the same as .delegate().

在这个例子.on()中与.delegate().