Can I open a dropdownlist using jQuery

后端 未结 14 1456
终归单人心
终归单人心 2020-11-22 09:49

For this dropdownlist in HTML:


I would lik

相关标签:
14条回答
  • 2020-11-22 09:50

    As has been stated, you can't programmatically open a <select> using JavaScript.

    However, you could write your own <select> managing the entire look and feel yourself. Something like what you see for the autocomplete search terms on Google or Yahoo! or the Search for Location box at The Weather Network.

    I found one for jQuery here. I have no idea whether it would meet your needs, but even if it doesn't completely meet your needs, it should be possible to modify it so it would open as the result of some other action or event. This one actually looks more promising.

    0 讨论(0)
  • 2020-11-22 09:51

    I was trying to find the same thing and got disappointed. I ended up changing the attribute size for the select box so it appears to open

    $('#countries').attr('size',6);
    

    and then when you're finished

    $('#countries').attr('size',1);
    
    0 讨论(0)
  • 2020-11-22 09:52

    This is spruced up from the answers just above and uses the length/number of options to conform to how many options there actually are.

    Hope this helps somebody get the results they need!

        function openDropdown(elementId) {
            function down() {
                var pos = $(this).offset(); // remember position
                var len = $(this).find("option").length;
                    if(len > 20) {
                        len = 20;
                    }
    
                $(this).css("position", "absolute");
                $(this).css("zIndex", 9999);
                $(this).offset(pos);   // reset position
                $(this).attr("size", len); // open dropdown
                $(this).unbind("focus", down);
                $(this).focus();
            }
            function up() {
                $(this).css("position", "static");
                $(this).attr("size", "1");  // close dropdown
                $(this).unbind("change", up);
                $(this).focus();
            }
            $("#" + elementId).focus(down).blur(up).focus();
        }
    
    0 讨论(0)
  • 2020-11-22 09:55

    I've come across the same problem and I have a solution. A function called ExpandSelect() that emulates mouse clicking on "select" element, it does so by creating an another <select> element that is absolutely posioned and have multiple options visible at once by setting the size attribute. Tested in all major browsers: Chrome, Opera, Firefox, Internet Explorer. Explanation of how it works, along with the code here:

    Edit (link was broken).

    I've created a project at Google Code, go for the code there:

    http://code.google.com/p/expandselect/

    Screenshots

    There is a little difference in GUI when emulating click, but it does not really matter, see it for yourself:

    When mouse clicking:


    (source: googlecode.com)

    When emulating click:


    (source: googlecode.com)

    More screenshots on project's website, link above.

    0 讨论(0)
  • 2020-11-22 09:56

    Maybe late, but this is how i solved it: http://jsfiddle.net/KqsK2/18/

    $(document).ready(function() {
      fixSelect(document.getElementsByTagName("select"));
                          });                       
    
       function fixSelect(selectList)
                {
                for (var i = 0; i != selectList.length; i++)
                  {
    
                     setActions(selectList[i]);
                  }
                }
    
    
       function setActions(select)
                {
                   $(select).click(function() {
                       if (select.getElementsByTagName("option").length == 1)
                            {
                              active(select);
                            }
                          });
                    $(select).focus(function() {
                           active(select);
                           });
                    $(select).blur(function() {
                           inaktiv(select);
                           });
                    $(select).keypress(function(e) {
                          if (e.which == 13) {
    
                          inaktiv(select);
                              }
                           });
                      var optionList = select.getElementsByTagName("option");
    
                      for (var i = 0; i != optionList.length; i++)
                               {
                                    setActionOnOption(optionList[i], select);
                               }
          }
    
      function setActionOnOption(option, select)
          {
                        $(option).click(function() {
                                    inaktiv(select);
                            });
          }
    
      function active(select)
          {
              var temp = $('<select/>');
                  $('<option />', {value: 1,text:$(select).find(':selected').text()}).appendTo(temp);
                   $(temp).insertBefore($(select));
    
                  $(select).attr('size', select.getElementsByTagName('option').length);
                  $(select).css('position', 'absolute');
                  $(select).css('margin-top', '-6px');
                  $(select).css({boxShadow: '2px 3px 4px #888888'});
    
    
    
                            }
    
            function inaktiv(select)
                       {
                     if($(select).parent().children('select').length!=1)
                         {
                                                 select.parentNode.removeChild($(select).parent().children('select').get(0));
                                    }
                    $(select).attr('size', 1);
                    $(select).css('position', 'static');
                    $(select).css({boxShadow: ''});
                    $(select).css('margin-top', '0px');
    
                      }
    
    0 讨论(0)
  • 2020-11-22 09:59

    I tried using mrperfect's answer and i had a couple glitches. With a couple small changes, I was able to get it to work for me. I just changed it so that it would only do it once. Once you exit dropdown, it would go back to the regular method of dropdowns.

    function down() {
        var pos = $(this).offset(); // remember position
        $(this).css("position", "absolute");
        $(this).offset(pos);   // reset position
        $(this).attr("size", "15"); // open dropdown
        $(this).unbind("focus", down);
    }
    function up() {
        $(this).css("position", "static");
        $(this).attr("size", "1");  // close dropdown
        $(this).unbind("change", up);
    }
    function openDropdown(elementId) {
        $('#' + elementId).focus(down).blur(up).focus();
    }
    
    0 讨论(0)
提交回复
热议问题