jQuery 如何触发 HTML Select onClick 事件(使用选项激活选择下拉列表)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19913171/
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-08-27 00:48:50  来源:igfitidea点击:

How to trigger the HTML Select onClick event (activates select dropdown with options)

javascriptjqueryhtmldom

提问by Paulo Roberto Rosa

I want to know how to trigger the onClickevent of any select(html combobox element).

我想知道如何触发onClick任何select(html 组合框元素)的事件。

I tried to do $('#MySelect').click();using jQueryand tried document.getElementById('MySelect').click();using pure javascript.

我尝试$('#MySelect').click();使用jQuery并尝试document.getElementById('MySelect').click();使用纯 javascript。

But the two don't fire the dropdown event that have the options of the select.

但是两者都不会触发具有选择选项的下拉事件。

Ps: i have sure that selector $('#MySelect')exists.

Ps:我确定选择器$('#MySelect')存在。

回答by Vicky Gonsalves

are you looking for this

你在找这个吗

  document.getElementById("MySelect").selectedIndex=3;

回答by tymeJV

Programatically triggering a clickevent will only run the defined click handler for that element. As you say in the comments, you have no such method defined, therefore no action will take place.

以编程方式触发click事件只会运行为该元素定义的点击处理程序。正如您在评论中所说,您没有定义这样的方法,因此不会发生任何操作。