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

时间:2021-02-15 00:05:35

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

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

I tried to do $('#MySelect').click(); using jQuery and 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')存在。

2 个解决方案

#1


4  

are you looking for this

你在找这个吗?

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

#2


0  

Programatically triggering a click event 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.

以编程方式触发单击事件将仅运行该元素的已定义单击处理程序。正如您在评论中所说,您没有定义此类方法,因此不会执行任何操作。

#1


4  

are you looking for this

你在找这个吗?

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

#2


0  

Programatically triggering a click event 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.

以编程方式触发单击事件将仅运行该元素的已定义单击处理程序。正如您在评论中所说,您没有定义此类方法,因此不会执行任何操作。