我试图编写一个jQuery,使页面加载时聚焦于第二个表单中第一个可见且启用的文本框或文本域。在下面的示例中,聚焦于form2中的输入text2。
jQuery动态聚焦于第一个输入框或文本域提出的解决方案对我不起作用。
请注意,我们的应用程序中有多个页面。在某些页面上,表单的第一个字段是文本框,在其他页面上,第一个字段是文本域。我想编写一个jQuery方法,它将适用于所有页面,无论第一个字段是文本框还是文本域。在下面的示例中,如果我们交换textarea1和text2字段,则解决方案应该适用。
<div id="header">
<form name="form1">
<input type="text" name="text1">
</form>
</div>
<div id="content">
<form name="form2">
<input type="checkbox" name="chc" value="test"><br>
<input type="hidden" name="hide">
<input type="text" name="text2"><br>
<textarea name="textarea1"></textarea><br>
<input type="text" name="text3">
</form>
</div>
这是我迄今为止尝试过的内容...
// throws the error - $("#content input:text, #content textarea").first is not a function
$("#content input:text, #content textarea").first().focus();
// focuses on the textarea even if a text box is the first element in the form
$("#content textarea, #content[type='text']:visible:enabled:first").focus();
// focuses on the last text box in the page!
$("#content :input[type='text'], :textarea:visible:enabled:first").focus();
// focuses on the first text box even if a textarea is the first element in the form
$("#content :input[type='text']:visible:enabled:first, :textarea:visible:enabled:first").focus();
// focuses on the checkbox as it is the first input element
$('#content :input:visible:enabled:first').focus();
谢谢。