Click trigger on select box doesn't work in jQuery
How can a dropdown list can be opened with a trigger?
Here is the code which doesn't work:
$('select').trigger('click');
Just for note - mousedown and mouseup also doesn't work.
$('select').children('option').each(function() {
if ($(this).is(':selected'))
{ $(this).trigger('change'); }
});
What you are trying to achieve is impossible. Even if you trigger a click the drop down list won't open like if the user clicked on it. If you want to change the currently selected value with a new one you could use the val function. I guess the only solution is to simulate the whole UI look and feel of a select
element using divs.
Took me a while but found a solution:
(Chrome & Safari ONLY)
function open(elem) {
if (document.createEvent) {
var e = document.createEvent("MouseEvents");
e.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
elem[0].dispatchEvent(e);
} else if (element.fireEvent) {
elem[0].fireEvent("onmousedown");
}
}
http://jsfiddle.net/oscarj24/GR9jU/
链接地址: http://www.djcxy.com/p/83922.html