jQuery:在表单内选择第一个子输入,但在类型:隐藏时不选择?
我有一个变量,它保存不同的表单对象,如下所示... $(formId)
每个表单都不同,因为要么有一个隐藏的输入字段作为第一个元素,要么第一个子元素是普通输入(类型="text"或其他)
我如何总是选择第一个“正常”输入字段
$(formId).find("input:first-child:not[type='hidden']")
我认为它应该是这样的。因此,如果您想知道我需要这个做什么 - 我想将焦点设置到表单中的第一个输入字段,但如果第一个输入是隐藏的,我当然想将其设置到下一个“可见”输入。
I have a variable that hold different form-objects like this … $(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)
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
:first-child
选择器选择第一个元素child,不是 jQuery 集合中的第一项。要选择集合的第一个元素,请使用 jQuery 的
:first
选择器。您对
:not
的实现也是错误的。:not[type='hidden']
等于:not()[type='hidden']
等于[type='hidden']
- 与您想要的相反。These are the right selectors (they're equivalent):
The
:first-child
selector selects an element which is the first child, not the first item in a jQuery collection.To select the first element of a collection, use jQuery's
:first
selector.Your implementation of
:not
is also wrong.:not[type='hidden']
is equal to:not()[type='hidden']
is equal to[type='hidden']
- The opposite of what you want.These are the right selectors (they're equivalent):
$(formId).find('input:visible:first').focus();
$(formId).find ('input:visible:first').focus();
使用 :hidden 选择器适合您的情况吗?
(不太确定我明白你的标记是什么样的......)
Would using the :hidden selector work in your case ?
(Not so sure I get what your markup looks like...)