jQuery 获取位于特定类中的所有文本字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6758301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:21:19  来源:igfitidea点击:

Get all text field value located in a specific class

jquery

提问by chhameed

I want to get all fields that are located 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 loopin which it return me the text fields value that is located in this class.. Any suggestions .. (i am new in jquery)

我想得到each loop它返回我位于这个类中的文本字段值的..任何建议..(我是jquery的新手)

回答by Igor Dymov

Try this:

尝试这个:

$(".test .text-field")

EDIT:

编辑:

To get values try this:

要获取值,请尝试以下操作:

$(".test .text-field").each(function() {
    alert($(this).val());
});

回答by Rodrigo Almeida

If you want all the values into an array, you can do this:

如果你想把所有的值都放到一个数组中,你可以这样做:

var texts= $(".test .text-field").map(function() {
   return $(this).val();
}).get();

回答by Grant Miller

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)

回答by cwallenpoole

Have you tried this:

你有没有试过这个:

$(".test input[type=\"text\"]")

回答by Sap

From cwallenpoole's answer following should work

从 cwallenpoole 的回答下面应该工作

$.each( $(".test input[type=\"text\"]"), function(index, ele){
   alert( ele.val());
 });