首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >jQuery:如何查找第一个可见的输入/选择/文本区域排除按钮?

jQuery:如何查找第一个可见的输入/选择/文本区域排除按钮?
EN

Stack Overflow用户
提问于 2010-05-13 07:22:37
回答 3查看 129.9K关注 0票数 89

我试过了

代码语言:javascript
复制
$(":input:not(input[type=button],input[type=submit],button):visible:first")

但它什么也找不到。

我的错误是什么?

更新:我在$(document).load()上执行此操作。

代码语言:javascript
复制
<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页面中。

到目前为止,我已经发现,对于某些元素,它确实可以找到它们(对于固定的元素),而对于某些元素,则不能。:(

EN

回答 3

Stack Overflow用户

发布于 2012-03-15 19:58:01

这是我对以上内容的总结,非常适合我。谢谢为我提供信息!

代码语言:javascript
复制
<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>
票数 7
EN

Stack Overflow用户

发布于 2016-01-31 02:27:01

这是对@Mottie的回答的改进,因为从jQuery 1.5.2开始,:text选择没有指定type属性的input元素(在这种情况下隐含type="text" ):

代码语言:javascript
复制
$('form').find(':text,textarea,select').filter(':visible:first')
票数 3
EN

Stack Overflow用户

发布于 2015-11-29 17:48:42

你可以试试下面的代码...

代码语言:javascript
复制
$(document).ready(function(){
    $('form').find('input[type=text],textarea,select').filter(':visible:first').focus();
})
代码语言:javascript
复制
<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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2823471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档