6

我没有如下下拉列表

<select name="dropname[0]">....</select>
<select name="dropname[1]">....</select>
<select name="dropname[2]">....</select>
<select name="dropname[3]">....</select>....

现在我想把焦点放在第二个下拉列表上。我试过这些

document.getElementsByName('dropname')[1].focus();

导致此错误

TypeError: document.getElementsByName(...)[1] is undefined

提前致谢

4

5 回答 5

6

由于您使用 jQuery 标记了您的问题,请尝试类似

$('select[name^="dropname"]').eq(1).focus();

// $('select[name^="dropname"]') < elements whos name starts with "dropname"
// .eq(1) < select one based on its index
// .focus(); < use jQuery's focus method to set the focus
于 2013-10-01T12:15:25.140 回答
6

尝试使用 jquery focus 像

$("#id2").focus();
于 2013-10-01T12:18:29.390 回答
2

您可以使用

var i = 0 //or 1,2,3

document.getElementsByName('dropname['+i+']')[0].focus();

希望对你有帮助

于 2013-10-01T12:19:14.363 回答
0
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> 
</script>
</head>
<body>
<select name="1">
    <option>1</option>
</select>
<select name="2">
    <option>2</option>
</select>
<select name="3">
    <option>3</option>
</select>
<select name="4">
    <option>4</option>
</select>
<select name="5">
    <option>5</option>
</select>
<script>
$(document).ready(function(){
 i=2;
 $('select[name="'+i+'"]').focus();
});
</script>
</body>
</html>

希望对你有帮助。!

于 2020-02-25T12:08:10.317 回答
0

通过点击正确关注元素的最简单方法是

$('#nights').show().focus().click();
于 2021-10-05T06:49:27.703 回答