且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将重点放在特定的选择框上

更新时间:2023-02-13 07:51:41

如果您希望用户将注意力集中在多个选择中的第一个,则可以使用以下jQuery代码:

If you want the user to have focus on the first of multiple select's, you can use the following jQuery code:

如果您具有首次选择的ID,则可以使用以下命令直接访问该元素:

If you have an id for the first select, you can directly access that element with the following:

$(document).ready(function () {
    $('#idOfFirstSelect').focus();
}); 

但是,如果您没有ID,则以下代码应该可以使用.

However, if you don't have the id, the following code should work.

演示: http://jsfiddle.net/biz79/1qo6mxnf/

$(document).ready(function () {
    $('select').first().focus();
});

另外,如果您还希望选择一个默认选项,则可以在HTML中将该选项设置为"selected":

On a separate note, if you also want to have a default option selected, you can set that option to "selected" in the HTML:

<option value="saab" selected="selected">Saab</option>