javascript - 如何执行 setfocus();到下拉列表

标签 javascript drop-down-menu setfocus

我有一个下拉列表,它在 OnLoad() 事件上从数据库加载记录,我想在页面加载时将焦点设置在下拉框上。我尝试了很多方法来设置焦点,但它不起作用。请看下面的代码。

HTML代码如下,

 <body onLoad="getDataSourceList()">
<div class="wrapper">   


    <form id="myUploadForm" name="myUploadForm">

    <table width="100%">
            <tr> <td align="right"><label for="dataSourceId" style=" font-family: serif; font-size: small;">DataSource Type<b style="color: red">*</b>: </label> </td> 
                 <td><select name="dataSourceType" id="dataSourceId">
                        <option value="Select"> Select</option>
                    </select>
                 </td> 
            </tr>

        </table>

    </form>


我的 ajax 代码如下:

function getDataSourceList() {
    $.ajax({
        url : '/DataWeb/getAllDataSources',
        type : 'post',
        dataType : 'json',
        contentType : 'application/json',

        success : function(map) {
            console.log(map);
            var select = document.getElementById("dataSourceId");
            for (index in map) {
                select.options[select.options.length] = new Option(
                        map[index], index);
            }
        },

        error : function(map) {
            console.log(map);
            console.log("error occured!!!");
        },

    });
    document.getElementById('dataSourceId').focus();
}

提前致谢。

最佳答案

$(document).ready(function(){
 $("#myid").focus();
});

这对我有用...

关于javascript - 如何执行 setfocus();到下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13226888/

相关文章:

html - 纯 CSS 选择菜单/下拉菜单 : How to make right arrow function?

javascript - 如何在播放视频时隐藏标题?

javascript - 在网页上截取 Adob​​e flash 播放器的屏幕截图

javascript - 使用 jquery 进行下拉列表验证

flash - 下拉菜单出现在 Flash 视频后面

input - 使用 Ionic 2 将焦点设置在输入上

javascript - 刷新iframe而不影响没有iframe id的父页面

javascript - Zurb 基金会 : drop up menus with bottom fixed navbar (rather than dropdown)

python - 如何在应用程序启动时将焦点设置在小部件上?

vb.net - 将焦点设置到 Web 浏览器控件中的 HTML 文本框和按钮