获取具有特定标签的子元素 jQuery
我试图通过仅提供表单的名称并且只知道那些想要的字段是输入元素来从 jQuery 获取特定表单的所有输入元素。
比方说:
<form action='#' id='formId'>
<input id='name' />
<input id='surname'/>
</form>
如何使用 jQuery 单独访问它们? 我尝试了类似 $('#formId > input')
的方法,但没有成功,实际上控制台上返回了一个错误 “XML 过滤器应用于非 XML 值(函数( a, b) {return new (c.fn.init)(a, b);})"
也许我必须用 .children 或类似的东西来做?我对 jQuery 还很陌生,而且我不太喜欢这些文档。 Mootools 更友好,或者也许我只是需要习惯它。
哦,最后但并非最不重要的一点是,我之前见过它问过,但没有最终答案,我可以使用 jQuery 创建一个新的 dom 元素并在将它插入(如果我这样做的话)到代码中之前使用它吗?在mootools中,我们有类似 var myEl = new Element(element[,properties]);
然后你可以在进一步的表达式中引用它,但我无法理解如何在 jQuery 上做到这
一点我最终做的是这样的: $('#where').before("Link Text")
但这违背了在插入它之前使用它的要求,如果你知道我的意思的话。
提前致谢。
I'm trying to get all the input elements from a certain form from jQuery by providing only the name of the form and only knowing that those wanted fields are input elements.
Let's say:
<form action='#' id='formId'>
<input id='name' />
<input id='surname'/>
</form>
How do I access them individually with jQuery?
I tried something like $('#formId > input')
with no success, in fact an error came back on the console "XML filter is applied to non-XML value (function (a, b) {return new (c.fn.init)(a, b);})"
Maybe I have to do it with .children or something like that? I'm pretty new at jQuery and I'm not really liking the Docs. It was much friendlier in Mootools, or maybe I just need to get used to it.
Oh and last but not least, I've seen it asked before but no final answer, can I create a new dom element with jQuery and work with it before inserting it (if I ever do) into de code? In mootools, we had something like var myEl = new Element(element[, properties]);
and you could then refer to it in further expressions, but I fail to understand how to do that on jQuery
What I ended up doing was something like this: $('#where').before("<a id='linkId' href='#'>Link Text</a>")
but this defeats the requirement of working with it before inserting it if you know what I mean.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想要所有后代,那么 @woland 的答案有效。如果您确实只想要您的 > 所示的直系子代,然后
Wolands 匹配名字、姓氏和电话。我的只匹配名字和姓氏
If you want all descendants then @woland's answer works. If you really only want the direct children as indicated by your > then
Wolands matches name, surname and phone. Mine matches just name and surname
我希望这能回答您的问题。
I hope this answers your questions.
它的工作原理如下:
Here's how it works:
如果您想循环遍历所有输入,请查看
each()
jQuery 中的函数:If you want to loop through all of the inputs, take a look at the
each()
function in jQuery: