jQuery Selecting the first child with a specific attribute

别说谁变了你拦得住时间么 提交于 2019-12-04 02:50:36

问题


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.


回答1:


Try:

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



回答2:


how about the first selector

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


来源:https://stackoverflow.com/questions/1766652/jquery-selecting-the-first-child-with-a-specific-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!