最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to trigger the HTML Select onClick event (activates select dropdown with options) - Stack Overflow

programmeradmin3浏览0评论

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

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

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

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

Share Improve this question asked Nov 11, 2013 at 18:23 Paulo Roberto RosaPaulo Roberto Rosa 3,2955 gold badges31 silver badges53 bronze badges 10
  • 1 $( "#MySelect" ).trigger( "click" ); I think this is what you're looking for – em_ Commented Nov 11, 2013 at 18:24
  • Do you have a defined onclick event? Or an onchange event? – tymeJV Commented Nov 11, 2013 at 18:26
  • Using pure javascript this is how you would do it: stackoverflow./questions/7914684/… – megawac Commented Nov 11, 2013 at 18:26
  • @ElliotM i tried this just now, with multiple selects, created another page with another new select too, did a lot of tests using this way and i't dont works. – Paulo Roberto Rosa Commented Nov 11, 2013 at 18:29
  • @tymeJV no. i did not. – Paulo Roberto Rosa Commented Nov 11, 2013 at 18:29
 |  Show 5 more ments

2 Answers 2

Reset to default 6

are you looking for this

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

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论