jQuery:如何找到不包括按钮的第一个可见输入/选择/文本区域?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2823471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:20:14  来源:igfitidea点击:

jQuery: how to find first visible input/select/textarea excluding buttons?

jqueryinputjquery-selectors

提问by Artem

I tried

我试过

$(":input:not(input[type=button],input[type=submit],button):visible:first")

but it doesn't find anything.

但它没有找到任何东西。

What is my mistake?

我的错误是什么?

UPD: I execute this on $(document).load()

UPD:我在 $(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>

and in the debug I can see that firstInput is empty.

在调试中我可以看到 firstInput 是空的。

UPD2: I'm in ASP.NET page running under Sharepoint.

UPD2:我在Sharepoint 下运行的ASP.NET 页面中。

I've found so far that for some elements it does find them (for fixed ones) and for some don't. :(

到目前为止,我发现对于某些元素,它确实找到了它们(对于固定元素),而对于某些元素则没有。:(

回答by Mottie

Why not just target the ones you want (demo)?

为什么不只针对你想要的(演示)?

$('form').find('input[type=text],textarea,select').filter(':visible:first');

Edit

编辑

Or use jQuery :inputselector to filter form descendants.

或者使用 jQuery :input选择器来过滤表单后代。

$('form').find('*').filter(':input:visible:first');

回答by PeterFromCologne

The JQuery code is fine. You must execute in the ready handler not in the window load event.

JQuery 代码很好。您必须在就绪处理程序中执行,而不是在窗口加载事件中执行。

<script type="text/javascript">
$(function(){
  var aspForm  = $("form#aspnetForm");
  var firstInput = $(":input:not(input[type=button],input[type=submit],button):visible:first", aspForm);
  firstInput.focus();
});
</script>

Update

更新

I tried with the example of Karim79(thanks for the example) and it works fine: http://jsfiddle.net/2sMfU/

我尝试使用 Karim79 的示例(感谢示例)并且它工作正常:http: //jsfiddle.net/2sMfU/

回答by Ben Foster

This is my summary of the above and works perfectly for me. Thanks for the info!

这是我对上述内容的总结,非常适合我。谢谢(你的)信息!

<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>

回答by Mike Slinn

This is an improvement over @Mottie's answer because as of jQuery 1.5.2 :textselects inputelements that have no specified typeattribute (in which case type="text"is implied):

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

$('form').find(':text,textarea,select').filter(':visible:first')

回答by J Adam Rogers

Here is my solution. The code should be easy enough to follow but here is the idea:

这是我的解决方案。代码应该很容易理解,但这里的想法是:

  • get all inputs, selects, and textareas
  • filter out all buttons and hidden fields
  • filter to only enabled, visible fields
  • select the first one
  • focus the selected field
  • 获取所有输入、选择和文本区域
  • 过滤掉所有按钮和隐藏字段
  • 过滤到仅启用的可见字段
  • 选择第一个
  • 聚焦所选字段

The code:

编码:

function focusFirst(parent) {
    $(parent).find('input, textarea, select')
        .not('input[type=hidden],input[type=button],input[type=submit],input[type=reset],input[type=image],button')
        .filter(':enabled:visible:first')
        .focus();
}

Then simply call focusFirst with your parent element or selector.

然后只需使用您的父元素或选择器调用 focusFirst。

Selector:

选择器:

focusFirst('form#aspnetForm');

Element:

元素:

var el = $('form#aspnetForm');
focusFirst(el);

回答by Mahbub

You may try below code...

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

$(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>