Dropdown not opening when using focus and focusout

丶灬走出姿态 提交于 2019-12-12 05:48:01

问题


I have two dropdowns with same the same class let’s call it dropdown and I do this fiddle with the dropdowns with jquery:

$('.dropdown').focus(function () {
    //Fiddle with this dropdown
}).focusout(function () {
    //Fiddle with this dropdown
});

When I jump between two dropdowns with the same class (dropdown) the drop down doesn’t open immediately instead it get focus and I have to click on it again in order to open it up. It seem to have something to do with the fact that I enable and disable options in the list when I fiddle with it. Is there a workaround?

(I have tried to use blur instead of focusout and the same problem occur)

Thanks!

Edit:

jsfiddle

JS:

$('.dropdown').focus(function () {
    var selectListIndex = $(this).attr('selectedIndex');
    $('.dropdown').each(function() {
        $('option:nth-child(' + (selectListIndex + 1) + ')', $(this)).attr('disabled', '');
    });
}).focusout(function () {
    var selectListIndex = $(this).attr('selectedIndex');
    $('.dropdown').not($(this)).each(function() {
        $('option:nth-child(' + (selectListIndex + 1) + ')', $(this)).attr('disabled', 'disabled');
    });
});

HTML:

<select class="dropdown">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>
<select class="dropdown">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>

回答1:


try this

$('.dropdown').focus(function () {
    $('.dropdown').focusout() 
});


来源:https://stackoverflow.com/questions/6517582/dropdown-not-opening-when-using-focus-and-focusout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!