Can I open a dropdownlist using jQuery

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

For this dropdownlist in HTML:


I would lik

相关标签:
14条回答
  • 2020-11-22 10:02

    i just added

    select = $('#' + id);
    length = $('#' + id + ' > option').length;
    if (length > 20)
        length = 20;
    select.attr('size', length);
    select.css('position', 'absolute');
    select.focus();
    

    and add into the select

    onchange="$(this).removeAttr('size');"
    onblur="$(this).removeAttr('size');"
    

    to make the same appearance like the classic one (overlap the rest of html)

    0 讨论(0)
  • 2020-11-22 10:03

    This should cover it:

     var event;
     event = document.createEvent('MouseEvents');
     event.initMouseEvent('mousedown', true, true, window);
     countries.dispatchEvent(event); //we use countries as it's referred to by ID - but this could be any JS element var
    

    This could be bound for example to a keypress event, so when the element has focus the user can type and it will expand automatically...

    --Context--

      modal.find("select").not("[readonly]").on("keypress", function(e) {
    
         if (e.keyCode == 13) {
             e.preventDefault();
             return false;
         }
         var event;
         event = document.createEvent('MouseEvents');
         event.initMouseEvent('mousedown', true, true, window);
         this.dispatchEvent(event);
     });
    
    0 讨论(0)
  • 2020-11-22 10:03

    Simple an easy way.

    function down(what) {
      pos = $(what).offset();  // remember position
      $(what).css("position","absolute");
      $(what).offset(pos);   // reset position
      $(what).attr("size","10"); // open dropdown
    }
    
    function up(what) {
    $(what).css("position","static");
    $(what).attr("size","1");  // close dropdown
    }
    

    Now you can call your DropDown just like this

    <select onfocus="down(this)" onblur="up(this)">
    

    Works perfect for me.

    Maybe better, because you have no problems with the position of the other elemts on the page.

    function down(was) {
    a = $(was).clone().attr('id','down_man').attr('disabled',true).insertAfter(was);
    $(was).css("position","absolute").attr("size","10");
    }
    
    function up(was) {
    $('#down_man').remove();
    $(was).css("position","static");
    $(was).attr("size","1");
    }
    

    Change the ID to a fix value mybe not smart but i hope you see the idee.

    0 讨论(0)
  • 2020-11-22 10:03

    No you can't.

    You can change the size to make it larger... similar to Dreas idea, but it is the size you need to change.

    <select id="countries" size="6">
      <option value="1">Country 1</option>
      <option value="2">Country 2</option>
      <option value="3">Country 3</option>
      <option value="4">Country 4</option>
      <option value="5">Country 5</option>
      <option value="6">Country 6</option>
    </select>
    
    0 讨论(0)
  • 2020-11-22 10:05

    Super simple:

    var state = false;
    $("a").click(function () {
        state = !state;
        $("select").prop("size", state ? $("option").length : 1);
    });
    
    0 讨论(0)
  • 2020-11-22 10:10

    You can easily simulate a click on an element, but a click on a <select> won’t open up the dropdown.

    Using multiple selects can be problematic. Perhaps you should consider radio buttons inside a container element which you can expand and contract as needed.

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