[英]Looping into a Table and getting values from different Inputs inside the Columns
I have a Dynamic Table which can have an undefined number of rows, every row with a defined number of Columns and inside every column, always the same Input/Control, like this: 我有一个动态表,该表可以具有未定义的行数,每行具有定义的列数,并且在每列内部,总是相同的输入/控件,如下所示:
<table id='UndefinedTable'>
<tr>
<td><input type='text'></td> <!-- DataBase: Table Column1 -->
<td><input type='text'></td> <!-- DataBase: Table Column2 -->
<td><input type='date'></td> <!-- DataBase: Table Column3 -->
<td><textarea></textarea></td> <!-- DataBase: Table Column4 -->
</tr>
<tr>
<td><input type='text'></td> <!-- DataBase: Table Column1 -->
<td><input type='text'></td> <!-- DataBase: Table Column2 -->
<td><input type='date'></td> <!-- DataBase: Table Column3 -->
<td><textarea></textarea></td> <!-- DataBase: Table Column4 -->
</tr>
<!-- This table may have an undefined number of rows -->
</table>
Also, rows can be erased (I figured a way to loop into it, by giving number values to every row, tr1, tr2, tr3, but as i said, any row can be erased by the user, also, was a really dirty solution). 另外,可以删除行(我想出了一种循环的方法,可以为每行tr1,tr2,tr3提供数值,但是正如我所说,用户可以擦除任何行,这也是很脏的事情。解)。
I need to obtain all the values/text inside every control and send the data through ajax to a PHP File for to make an insert of every row. 我需要获取每个控件中的所有值/文本,并通过ajax将数据发送到PHP文件,以对每一行进行插入。
I been thinking on some ways to do this, but the problem is that I don't have idea how to loop into the table and get the values of the Inputs and the TextArea. 我一直在想办法做到这一点,但问题是我不知道如何循环到表中并获取Inputs和TextArea的值。
If you would like a pure JavaScript approach as well you could give each input a name attribute dynamically and loop through them, putting the values into an array or a JSon object to be passed to your server using Ajax. 如果您也希望使用纯JavaScript方法,则可以动态地为每个输入提供一个name属性,并在它们之间循环,将值放入数组或JSon对象中,以使用Ajax传递给服务器。
var inputs = document.getElementsByName('input');
var values = [];
for (var i = 0; i < inputs.length; i++) {
values.push(inputs[i].value);
}
To give your Textbox's and Textarea's a name you can use: 要给您的文本框和文本区域命名,可以使用:
.setAttribute('name', 'input');
You could select all of these elements with 您可以选择所有这些元素
$("#UndefinedTable input, #UndefinedTable textarea") //or this...
$("#UndefinedTable").find("input, textarea") //...alternative version
and loop through them: 并遍历它们:
$("#UndefinedTable input, #UndefinedTable textarea").each(function(){
alert($(this).val());
});
var formDATA = {};
$("#UndefinedTable tr").each(function(k){
var obj = {};
$(this).find("input,textarea").each(function(i){
obj["field"+i] = $(this).val();
});
formDATA["row"+k] = obj;
});
//console.log(formDATA);
if you add attribute name
to input and textarea you can change this line: 如果将属性
name
添加到输入和文本区域,则可以更改此行:
obj["field"+i] = $(this).val();
with: 有:
obj[$(this)] = $(this).val();
Now, you you can send formDATA
via $.ajax() as data
: 现在,您可以通过$ .ajax()作为
data
发送formDATA
:
$.ajax({
//...
data : formDATA
//...
})
First you must change your html, like: 首先,您必须更改html,例如:
<form id="form1" action="/post.php" method="post">
<table id='UndefinedTable'>
<tr>
<td><input type='text'></td> <!-- DataBase: Table Column1 -->
<td><input type='text'></td> <!-- DataBase: Table Column2 -->
<td><input type='date'></td> <!-- DataBase: Table Column3 -->
<td><textarea></textarea></td> <!-- DataBase: Table Column4 -->
</tr>
<tr>
<td><input type='text'></td> <!-- DataBase: Table Column1 -->
<td><input type='text'></td> <!-- DataBase: Table Column2 -->
<td><input type='date'></td> <!-- DataBase: Table Column3 -->
<td><textarea></textarea></td> <!-- DataBase: Table Column4 -->
</tr>
<!-- This table may have an undefined number of rows -->
</table>
<input value="send" type="submit">
</form>
now the JS: 现在的JS:
$("#form1").on("submit.block",function(e){
e.preventDerault();
});
$("#UndefinedTable tr").each(function(k){
$(this).find("input,textarea").each(function(i){
$(this).attr("name","row"+k+"[field"+i+"]");
});
});
$("#form1").off("submit.block");
if you post the form to a php file and use var_dump($_POST)
, the script PRINT an array like: 如果将表单发布到php文件并使用
var_dump($_POST)
,则脚本将打印数组,如下所示:
array (size=2)
'row0' =>
array (size=4)
'field0' => string 'Field1' (length=6)
'field1' => string 'field2' (length=6)
'field2' => string '2015-01-14' (length=10)
'field3' => string 'row1' (length=4)
'row1' =>
array (size=4)
'field0' => string 'Field1' (length=6)
'field1' => string 'field2' (length=6)
'field2' => string '2015-01-23' (length=10)
'field3' => string 'row2' (length=4)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.