最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Selecting the first child with a specific attribute - Stack Overflow

programmeradmin2浏览0评论

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>

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.

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>

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.

Share Improve this question edited Nov 30, 2009 at 13:25 AnthonyWJones 189k35 gold badges235 silver badges307 bronze badges asked Nov 19, 2009 at 21:18 kingrichard2005kingrichard2005 7,26921 gold badges89 silver badges120 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 36

Try:

$(this).children("input[type='submit']:first").val();

how about the first selector

var name = $("input[type='submit']:first").val();
发布评论

评论列表(0)

  1. 暂无评论