jQuery选择具有特定属性的第一个子节点 [英] jQuery Selecting the first child with a specific attribute
本文介绍了jQuery选择具有特定属性的第一个子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个HTML格式的表单,其中包含多个提交类型的输入:
I have a form in HTML with multiple inputs of type submit:
<form id = "myForm1" action="doSomethingImportant/10" class="postLink" method="post">
<input type="hidden" id="antiCSRF" name="antiCSRF" value="12345"></input>
<input type="submit" value="clickThisLink"></input>
<input type="submit" value="Don'tclickThisLink"></input>
</form>
我想做的是只选择类型为submit的第一个输入而忽略其他输入,片段我目前拥有的代码如下所示,请注意它在for-each循环中遍历我页面上的所有表单,因此(这)避免混淆:
What I want to do is select only the first input with type submit while ignoring the others, the snippet of code I currently have is as follows, note it is within a for-each loop that goes through all forms on my page, hence (this) to avoid confusion:
var name = $(this).find("input[type='submit']").val();
我认为这已经抓住了默认情况下提交类型的第一个输入,我不确定如果这个假设是正确的,或者有更正式的方法,谢谢。
I'm thinking this already grabs the first input of type submit by default, I'm not sure if that assumption is correct or if there's a more formal way of going about it, thanks.
推荐答案
尝试:
$(this).children("input[type='submit']:first").val();
这篇关于jQuery选择具有特定属性的第一个子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文