jQuery open select by button

前端 未结 7 1103
小蘑菇
小蘑菇 2021-01-11 15:33

How to open select dropdown by button?

$(\'button\').on(\'click\', function() {
   $(\'select\').trigger(\'click\');
});

My code: http://js

7条回答
  •  北海茫月
    2021-01-11 16:01

    (function($) {
        "use strict";
        $.fn.openSelect = function()
        {
            return this.each(function(idx,domEl) {
                if (document.createEvent) {
                    var event = document.createEvent("MouseEvents");
                    event.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                    domEl.dispatchEvent(event);
                } else if (element.fireEvent) {
                    domEl.fireEvent("onmousedown");
                }
            });
        }
    }(jQuery));
    $('#country').openSelect();
    

    http://jsfiddle.net/yqs90jdw/

提交回复
热议问题