开发者

jQuery change select list selectedIndex on hover event and also send click?

开发者 https://www.devze.com 2023-01-31 10:54 出处:网络
I have an image on my page that when hovered changes the selectedIndex of a select list.开发者_开发问答

I have an image on my page that when hovered changes the selectedIndex of a select list.

开发者_开发问答
// change the selected index of the select list on element hover
$("#mySelectList").attr('selectedIndex', 1); // works

The select list also fires another function on change. This works when I manually change the options from the select list but not when I change the selectedIndex on the hover event (code above).

My question, is there a way that I can also send a "click" event or something similar? Basically I want to be able to change the selectedIndex of the select list and also have it fire the other function.


What you can try is to trigger the change (or click or whetever) event.

$('#mySelectList').trigger('change');

or

$('#mySelectList').trigger('click');

after you've set the selected index


You can fire the onchange event of the select control:

How do I programmatically force an onchange event on an input?

Just use:

$("#mySelectList").onchange()


Why not refactor the function that you would perform normally in the click event into a JavaScript function and then call it directly?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号