简体   繁体   English

如何使用jQuery获取所有文本输入值?

[英]How to get all values of text inputs with jQuery?

I have a table with a column of pre-populated text inputs: 我有一个表格,其中包含一列预先填充的文本输入:

    <table><tr><td>...</td><td><input class="unique_class" value="value1"/></td></tr>...

I gave each input box the same css class that only that element has (unique_class). 我给每个输入框提供了同一个css类,只有那个元素有(unique_class)。 I was hoping that I could use jQuery.val() to grab an array of all the input values with the css class in the table like this (I have done something similar with checkbox values): 我希望我可以使用jQuery.val()来获取表中css类的所有输入值的数组(我已经用复选框值做了类似的事情):

    var values = $('.unique_class').val();

However, I only am getting the value of the last input in the table. 但是,我只获取表中最后一个输入的值。 Is there a simple 'fast' way to this without a for-loop iteration? 没有for循环迭代,是否有一种简单的“快速”方法? This table can get be very large (100+ rows)? 这个表可以变得非常大(100多行)?

To get an array of values you should use $.map method: 要获取值数组,您应该使用$.map方法:

var values = $('.unique_class').map(function() {
    return $(this).val();
}).get();

Also note, how you use .get method to convert jQuery array-like collection to actual array. 另请注意,如何使用.get方法将类似jQuery数组的集合转换为实际数组。

也许你可以试试这个:

 var values = $('td.unique_class').val();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM