jQuery:在表单中选择第一个子输入,但在输入时不选择:隐藏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9342636/
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
jQuery: select first-child input inside form but not when type:hidden?
提问by matt
I have a variable that hold different form-objects like this … $(formId)
我有一个变量,可以保存像这样的不同表单对象…… $(formId)
Each form is different as in either has an hidden input field as first element or the first-child is a normal input (type="text" or something else)
每个表单都是不同的,因为要么有一个隐藏的输入字段作为第一个元素,要么第一个孩子是一个正常的输入(type =“text”或其他东西)
how can I ALWAYS select the the first "normal" input field
我如何始终选择第一个“正常”输入字段
$(formId).find("input:first-child:not[type='hidden']")
I thought it should be something like this. So if you wonder what I need this for - I want to set the focus to the first input field in the form, but if the first input is a hidden one I of course want to set it to the next "visible" input.
我认为它应该是这样的。因此,如果您想知道我需要它做什么 - 我想将焦点设置到表单中的第一个输入字段,但是如果第一个输入是隐藏的,我当然希望将它设置为下一个“可见”输入。
回答by Rob W
The :first-child
selector selects an element which is the first child, notthe first item in a jQuery collection.
To select the first element of a collection, use jQuery's :first
selector.
所述:first-child
选择器选择其中元件是所述第一子,未在一个jQuery集合中的第一项。
要选择集合的第一个元素,请使用 jQuery 的:first
选择器。
Your implementation of :not
is also wrong.:not[type='hidden']
is equal to:not()[type='hidden']
is equal to[type='hidden']
- The oppositeof what you want.
你的实现:not
也是错误的。:not[type='hidden']
等于:not()[type='hidden']
等于[type='hidden']
-与您想要的相反。
这些是正确的选择器(它们是等效的):
input:not([type=hidden]):first
input[type!=hidden]:first
回答by David says reinstate Monica
$(formId).find ('input:visible:first').focus();
$(formId).find ('input:visible:first').focus();
回答by phtrivier
Would using the :hidden selector work in your case ?
使用 :hidden 选择器是否适用于您的情况?
$(formId).find("input:first-child:not(:hidden)")
(Not so sure I get what your markup looks like...)
(不太确定我明白你的标记是什么样的......)