Close a SELECT dropdown list programmatically with

2019-01-15 11:46发布

I have a dropdown that is initialized with one single value. When the user clicks it, the single element is removed and a new element is added saying "Loading", then an AJAX call is issued to the server and when returns, the new values are added to the control.

The problem is that the control remains open while updating, and I would like to close it.

This is an example: http://jsfiddle.net/vtortola/CGuBk/2/

The example's AJAX does not get data probably because something wrong I am doing when calling the jsfiddle api, but it shows how the SELECT remains open during update.

I want to know how to close the dropdown list programmatically w/o focus in another input.

9条回答
Luminary・发光体
2楼-- · 2019-01-15 12:27

SOLUTION 1 I found very easy solution.

select.style.display = "none";
setTimeout(function () {
    select.style.display = "block";
}, 10);

This hide element in DOM, this cause that dropdown will be closed and then with 10ms delay (without delay it does not work) return it into DOM.

SOLUTION 2: Little bit more complicated but without delay is totaly remove element from DOM and then immediately return it back.

var parent = select.parentElement;
var anchor = document.createElement("div");
parent.insertBefore(anchor, select);
parent.removeChild(select);
parent.insertBefore(select, anchor);
parent.removeChild(anchor);

This stores parrent of element, then bring anchor before select. Anchor is there for restoring select in the same position in DOM then before. Of course it can be implemented to function select element.

HTMLSelectElement.prototype.closeDropdown = function () {
    var parent = this.parentElement;
    var anchor = document.createElement("div");
    parent.insertBefore(anchor, this);
    parent.removeChild(this);
    parent.insertBefore(this, anchor);
    parent.removeChild(anchor);
}

and then just call

select.closeDropdown();

Example

查看更多
甜甜的少女心
3楼-- · 2019-01-15 12:28

After...

$select.html('<option value="-1">Loading</option>');

Try adding...

$select.blur();
查看更多
闹够了就滚
4楼-- · 2019-01-15 12:30
$('.select').on('change', function () {
    $(this).click();
});
查看更多
倾城 Initia
5楼-- · 2019-01-15 12:30

In case anyone else out there is using Angular2, the solution that worked there for me was to add a (focus) event that calls $($event.srcElement).attr("disabled","disabled");

I then add a timeout to re-enable the element when I want it to become active again.

查看更多
等我变得足够好
6楼-- · 2019-01-15 12:40

I'm not sure about anyone else, but I'm using the latest stable build of Chrome, and none of the other answers involving .blur() work for me.

This question asks the inverse: Programmatically open a drop-down menu

The conclusion that seemed to be obtained is that it's not possible due to the way the browser handles field element clicks.

A better solution would be to replace the dropdown with a pure HTML one and hide it when needed with a simple .hide() command.

查看更多
劳资没心,怎么记你
7楼-- · 2019-01-15 12:46

I know this is an old question and already have an answer but I think the following solution can be a fair way to achieve the goal.

You can simulate the closure of the select by re-rendering it. In jQuery you can implement a simple plugin to achieve that:

$.fn.closeSelect = function() {
    if($(this).is("select")){
        var fakeSelect = $(this).clone();
        $(this).replaceWith(fakeSelect);
    }
};

and use it this way:

$("#mySelect").closeSelect();
查看更多
登录 后发表回答