我试过了
$(":input:not(input[type=button],input[type=submit],button):visible:first")
但它什么也找不到。
我的错误是什么?
更新:我在$(document).load()上执行此操作。
<script type="text/javascript">
$(window).load(function () {
var aspForm = $("form#aspnetForm");
var firstInput = $(":input:not(input[type=button],input[type=submit],button):visible:first", aspForm);
firstInput.focus();
});
</script>
在调试中,我可以看到firstInput是空的。
UPD2:我在Sharepoint下运行的ASP.NET页面中。
到目前为止,我已经发现,对于某些元素,它确实可以找到它们(对于固定的元素),而对于某些元素,则不能。:(
发布于 2012-03-15 19:58:01
这是我对以上内容的总结,非常适合我。谢谢为我提供信息!
<script language='javascript' type='text/javascript'>
$(document).ready(function () {
var firstInput = $('form').find('input[type=text],input[type=password],input[type=radio],input[type=checkbox],textarea,select').filter(':visible:first');
if (firstInput != null) {
firstInput.focus();
}
});
</script>
发布于 2016-01-31 02:27:01
这是对@Mottie的回答的改进,因为从jQuery 1.5.2开始,:text
选择没有指定type
属性的input
元素(在这种情况下隐含type="text"
):
$('form').find(':text,textarea,select').filter(':visible:first')
发布于 2015-11-29 17:48:42
你可以试试下面的代码...
$(document).ready(function(){
$('form').find('input[type=text],textarea,select').filter(':visible:first').focus();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<form>
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
<input type="submit" />
</form>
https://stackoverflow.com/questions/2823471
复制相似问题