Open Select using Javascript/jQuery?

后端 未结 13 1737
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 13:57

Is there a way to open a select box using Javascript (and jQuery)?


                        
    
提交评论

  • 2020-11-28 14:44

    Okay, I found another way fixing this problem. Here is the fix:

    Please give me feedback! I'm kind of proud on myself ;)

    $(document).ready(function() {
        if (jQuery.browser.msie) {
                select_init();
        }
    });
    
    
    function select_init () {
        var selects = $('select');
        for (var i = 0; i < selects.length; i++) {
            _resizeselect.init(selects[i]);
        }
    }
    
    
    var _resizeselect = {
        obj : new Array(),
        init : function (el) {
            this.obj[el] = new resizeselect (el);
        }
    }
    
    function resizeselect (el) {
    
        this.el = el;
        this.p = el.parentNode;
        this.ht = el.parentNode.offsetHeight;
        var obj = this;
        this.set = false;
    
        el.onmousedown = function () {
            obj.set_select("mousedown");
        }
        el.onblur = function () {
            obj.reset_select("blur");
        }
        el.onchange = function () {
            obj.reset_select("change");
        }
    
    }
    
    resizeselect.prototype.set_select = function (str) {
    
        if (this.set) {
            this.set = false;
            return;
        }
    
        this.el.style.width = "auto";
        this.el.style.position = "absolute";
        this.p.style.height = this.ht + "px";
        this.p.style.zIndex = 100;
        this.set = true;
        this.el.focus();
    }
    
    resizeselect.prototype.reset_select = function (str) {
        this.el.style.width = "";
        this.el.style.position = "";
        this.p.style.height = "";
        this.p.style.zIndex = 1;
        this.set = false;
        window.focus();
    }
    
    0 讨论(0)
  • 2020-11-28 14:46

    I prefer to set my CSS in a CSS file and then "addClass" but even so, your code (portion)

       $('select').blur(function(){ 
            $(this).css({ 
                'position' : 'relative', 
                'width' : original_width 
            }); 
        }); 
    
        $('select').blur(function(){ 
            $(this).css({ 
                'position' : 'relative', 
                'width' : original_width 
            }); 
        }); 
    

    seems to be a duplicate

    I would make it:

    $('select').blur().css({ 
            'position' : 'relative', 
            'width' : original_width 
    }); 
    

    Not sure you really even need the .blur() here what with the .change() event (try taking it out see see if that addresses your issue...I use select often on IE and do not seem to have an issue.

    0 讨论(0)
  • 2020-11-28 14:49

    NO jQuery solution.

    <SCRIPT>
        function toggleDropdown(element){
            if(element.size == 0) {
                element.size = element.length;
                element.focus();
                }
            else element.size = 0;
            }
    </SCRIPT>
    

    Found this here.

    0 讨论(0)
  • 2020-11-28 14:50

    I think that you need to return true from your event handlers (click, blur, etc.) so after your handler executes, the browser continues to propagate the event and open the select.

    It is similar with href links, if they have an onclick handler and the handler returns false, the link is not followed (the browser stops the event after your handler executes).

    EDIT: Based on your comment and answer, it seems that your handler gets the first chance to execute only after the browser decides to open the box.
    I suggest that you try the focus event handler, it might get a chance to run earlier than the click handler and perhaps before the browser actually opens the box. It is also more consistent (applies both to mouse and keyboard navigation).

    0 讨论(0)
  • 2020-11-28 14:50

    There is an alternate solution i found for this problem. Just add a theme to your select box like Selectize.js it will convert your select box into ul li html tags but works as select box. You can easily hide show ul li's on jquery events.

    0 讨论(0)
  • 提交回复
    热议问题