I want to get all fields that are locat开发者_JAVA百科ed in a single class name, for example my code like.
<div class="test">
<input type="text" class="text-field" />
<input type="text" class="text-field" />
<input type="text" class="text-field" />
<input type="text" class="text-field" />
</div>
<div class="test">
<input type="text" class="text-field" />
<input type="text" class="text-field" />
<input type="text" class="text-field" />
<input type="text" class="text-field" />
</div>
I want to get the each loop
in which it return me the text fields value that is located in this class. Any suggestions?
Try this:
$(".test .text-field")
EDIT:
To get values try this:
$(".test .text-field").each(function() {
alert($(this).val());
});
If you want all the values into an array, you can do this:
var texts= $(".test .text-field").map(function() {
return $(this).val();
}).get();
Here's another method to obtain an array of the input values:
Array.from($('.test .text-field').get(), e => e.value)
Or alternatively:
[].map.call($('.test .text-field').get(), e => e.value)
Have you tried this:
$(".test input[type=\"text\"]")
From cwallenpoole's answer following should work
$.each( $(".test input[type=\"text\"]"), function(index, ele){
alert( ele.val());
});
精彩评论