[英]Selecting each input fields in the table column with jQuery
I want to select the input fields' values in each column. 我想在每一列中选择输入字段的值。
<table id="tablo">
<tr>
<td><input class="Name" type="text" /></td>
<td><input class="Age" type="text" /></td>
<td><input class="No" type="text" /></td>
</tr>
<tr>
<td><input class="Name" type="text" /></td>
<td><input class="Age" type="text" /></td>
<td><input class="No" type="text" /></td>
</tr>
<tr>
<td><input class="Name" type="text" /></td>
<td><input class="Age" type="text" /></td>
<td><input class="No" type="text" /></td>
</tr>
...
</table>
Then selecting each input value, I will create an object with these values and push the object in an array by looping through the selected inputs: 然后选择每个输入值,我将使用这些值创建一个对象,并通过遍历选定的输入将对象推入数组中:
var arr= []
var obj = {
Name: jquery selector will come here,
Age: jquery selector will come here,
No: jquery selector will come here
}
arr.push(obj);
I've tried with jQuery's $.each() function, but I just only reach the columns. 我已经尝试过使用jQuery的$ .each()函数,但是我只是到达列。
Id must be unique, so try it with using class like, ID必须是唯一的,因此请尝试使用类似的类,
var objs=[];
$('#tablo tr').each(function(){
var inputs = $(this).find('input'),o={};
inputs.each(function(){
o[$(this).attr('class')]=this.value;
});
objs.push(o);
});
Snippet, 片段
var objs = []; $('#tablo tr').each(function() { var inputs = $(this).find('input'), o = {}; inputs.each(function() { o[$(this).attr('class')] = this.value; }); objs.push(o); }); console.log(objs);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="tablo"> <tr> <td><input class="Name" type="text" value="test1"/></td> <td><input class="Age" type="text" value="123"/></td> <td><input class="No" type="text" value="no1" /></td> </tr> <tr> <td><input class="Name" type="text" /></td> <td><input class="Age" type="text" /></td> <td><input class="No" type="text" /></td> </tr> <tr> <td><input class="Name" type="text" /></td> <td><input class="Age" type="text" /></td> <td><input class="No" type="text" /></td> </tr> </table>
Try this: you can iterate earch row first and then read each input. 尝试以下操作:您可以先迭代earch行,然后读取每个输入。 Note: id must be unique throughout the DOM, hence replaced with classes 注意:id在整个DOM中必须是唯一的,因此被替换为class
$(function(){ var array = []; $('#tablo tr').each(function(){ var obj = {}; //obj['name']=$(this).find('td input:eq(0)').val(); //obj['age']=$(this).find('td input:eq(1)').val(); //obj['no']=$(this).find('td input:eq(2)').val(); //for dynamic build of object $(this).find('input').each(function(){ obj[$(this).attr('class')]=$(this).val(); }); array.push(obj); }); console.log(array); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="tablo"> <tr> <td><input class="Name" type="text" value="name1"/></td> <td><input class="Age" type="text" value="name1"/></td> <td><input class="No" type="text" value="name1"/></td> </tr> <tr> <td><input class="Name" type="text" value="name2"/></td> <td><input class="Age" type="text" value="age2"/></td> <td><input class="No" type="text" value="no2"/></td> </tr> <tr> <td><input class="Name" type="text" value="name3"/></td> <td><input class="Age" type="text" value="age3"/></td> <td><input class="No" type="text" value="no3"/></td> </tr> ... </table>
First replace input id's with classes of the same name. 首先用相同名称的类替换输入ID。 Then loop over rows like this: 然后像这样循环遍历行:
var arr = [];
$('#table tr').each(function(tr, index) {
var name = $(tr).find('.Name').val()
var age = $(tr).find('.Age').val()
var no = $(tr).find('.No').val()
arr.push({name:name, age:age, no:no});
})
You should fix the issue with the ids before (ids should be unique in the document) then with jquery will be as easy as: 您应该先用id来解决问题(id在文档中应该是唯一的),然后使用jquery就像这样简单:
const inputs = $('#tablo').find('input');
You even don't need jquery 您甚至不需要jquery
const inputs = document.querySelectorAll('#tablo input');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.