将重点放在特定的选择框上 [英] Set focus on specific select box

查看:97
本文介绍了将重点放在特定的选择框上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

页面上有多个选择框.我想将重点放在特定选择框的第一个元素上. 此处显示如何将焦点设置在选择框上.我也要说哪一个.我尝试使用下面的代码,但没有设置焦点.

I have multiple select boxes in the pages. I want to set focus on first element of specific select box. Here it shows how to set focus on select box. I need to say which one too. I tried below code but it didn't set the focus.

$('#thirdDropBox select:first').focus();

推荐答案

如果您希望用户将注意力集中在多个选择中的第一个,则可以使用以下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>

这篇关于将重点放在特定的选择框上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆