简体   繁体   English

如何获取表格列中多个文本框的值

[英]how to get value of multiple textboxes in table column

I am stuck with my project.我坚持我的项目。 On my jsp page, I've a html table .在我的jsp页面上,我有一个html table One column ie addresses has multiple textboxes with values.一列即addresses有多个带有值的textboxes Last column has a link named edit .最后一列有一个名为edit的链接。

What I want: on click of edit I want values of addresses separated by commas.我想要什么:点击编辑我想要用逗号分隔的地址值。

What I did (onClick edit):我做了什么(点击编辑):

var values = $(this).closest('tr').find('[type=text]').val();

What I get : only one value (not other textfield values)我得到的是:只有一个值(不是其他文本字段值)

What I want(example) : newyork, washington.我想要什么(示例):纽约,华盛顿。 (if there were 2 textboxes with newyork and washington as address values in addresses column) (如果addresses栏中有 2 个文本框,其中 newyork 和 Washington 作为地址值)

在此处输入图片说明

Accoring to http://api.jquery.com/val/ val() returns only the first value.根据http://api.jquery.com/val/ val() 仅返回第一个值。 If you want all of them, you need to iterate such as:如果您想要所有这些,则需要进行迭代,例如:

var s = []; 
$(this).closest('tr').find('[type=text]').each(function(v) { 
    s.push(v.val());
})

PS Array.prototype.map could be useful here PS Array.prototype.map 在这里很有用

To get the values of all the inputs you can use the map function to go over all of the inputs and then the get function to get all the values into an array:要获取所有输入的值,您可以使用map函数遍历所有输入,然后使用get函数将所有值放入数组中:

 var values = $('[type=text]').map(function() { return $(this).val(); }).get(); console.log(values);
 <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="a" value="a - val" /> <input type="text" name="b" value="b - val" />

Your code should look something like that:你的代码应该是这样的:

var values = $(this).closest('tr').find('[type=text]').map(function() {
    return $(this).val();
}).get();

And a better example after you added the image of your table:添加表格图像后的更好示例:

 $('#a1').click(function() { var values = $(this).closest('tr').find('[type=text]').map(function() { return $(this).val(); }).get(); alert(values); });
 <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td> <input type="text" name="a" value="a - val" /> <input type="text" name="b" value="b - val" /> </td><td> <div id="a1">edit</div> </td> </tr> </table>

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

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