简体   繁体   中英

How to Set Focus to a DropDownList control in a Modal Popup?

I have a bootstrap modal popup which has the first first a dropdownlist and I want the user to be able to table through the fields without having to use the mouse to set the focus in the modal popup. I can do this fine if the first field is a text box, but when it is a dropdownlist I can't get this to focus using the ddl_list.Focus() command.

This was solved after some further searching. Simply add the following javascript to the aspx file. This sets focus to the field field in a bootstrap popup modal.

<script type="text/javascript">
    $('body').on('shown.bs.modal', function (e) {
        var ele = $(e.target).find('input[type=text],textarea,select').filter(':visible:first'); // find the first input on the bs modal
        if (ele) {ele.focus();} // if we found one then set focus.
    })      
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM