动态添加数据到表行
这个问题与我之前在此链接中的问题相关。现在,我想做的是做相反的事情。我这里有一个动态表(您可以在其中添加和删除行),无论用户在最近使用表时输入的数据(通过按钮触发)都应该检索并发布在同一个表中(如果相同)按钮被点击)。
这是我用来从动态表中获取数据的代码:
$('#tableSearchMainAccount1 tr').each(function(){
var td = '';
if ($(this).find("td:first").length > 0) { // used to skip table header (if there is)
$(this).find('option:selected').each(function(){
td = td + $(this).text()+ ',';
});
td = td + $(this).find('input').val();
filt[ctr]=td;
ctr += 1;
}
});
alert (filt); //alert output like name,equals,ann,age,equals,11
现在,我想要做的是,当我再次单击该按钮时,先前的输入数据将再次发布到我的动态表中。
我的表格看起来像这样:
<table> <tr><th>field</th><th>comparison</th><th>value</th></tr>
<tr>
<td>
<select style="width:5em;" class="field">
<option>name</option>
<option>age</option>
<option>sex</option>
</select>
</td>
<td>
<select style="width:5em;" class = "comp">
<option>equals</option>
<option>starts with</option>
<option>not equal to</option>
</select>
</td>
<td><input type="text" class = 'value'></td>
<td><img class="add" src="css/images/add.png" /></td> // this is used to add new row
</tr>
</table>
当表格再次显示时,用户在表格中输入的最后一个数据应该显示在表格中。如何将数据添加到动态表中?
编辑:
单击该按钮时,将打开一个对话框,其中包含我的动态表,每次用户关闭该对话框时,该表中的所有行都将与用户输入的数据相同。我已经创建了一个变量来保存表中最新输入的数据。因此,当再次单击按钮时,我希望表格生成或显示用户最后输入的数据。输出应与最新输入数据相同。
This problem is related to my previous question in this link. Now, what I'm trying to do is do the opposite thing. I have here a dynamic table (where you can add and delete rows) and whatever is the data that the user input in the latest use of table (trigger by a button) should be retrieve and be posted in the same table (if the same button is clicked).
Here's the code that I used to get the data from my dynamic table:
$('#tableSearchMainAccount1 tr').each(function(){
var td = '';
if ($(this).find("td:first").length > 0) { // used to skip table header (if there is)
$(this).find('option:selected').each(function(){
td = td + $(this).text()+ ',';
});
td = td + $(this).find('input').val();
filt[ctr]=td;
ctr += 1;
}
});
alert (filt); //alert output like name,equals,ann,age,equals,11
Now, what I want to do is that when I click the button again, this previous input data will be posted again in my dynamic table.
My table is something that looks like this:
<table> <tr><th>field</th><th>comparison</th><th>value</th></tr>
<tr>
<td>
<select style="width:5em;" class="field">
<option>name</option>
<option>age</option>
<option>sex</option>
</select>
</td>
<td>
<select style="width:5em;" class = "comp">
<option>equals</option>
<option>starts with</option>
<option>not equal to</option>
</select>
</td>
<td><input type="text" class = 'value'></td>
<td><img class="add" src="css/images/add.png" /></td> // this is used to add new row
</tr>
</table>
The last data that a user input in the table should be displays in the table when table shows again. How do I add the data to my dynamic table?
EDIT:
When the button is clicked, a dialog opens which contains my dynamic table and every time a user close the dialog, all the row from that table will be gone same as the data the user entered. I already create a variable that holds the latest inputted data in the table. So when the button click again, I want the table to generate or displays the last input data by the user. The output should be the same as the latest input data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您有一个按钮 id
add
并且您想在末尾添加一个表格行:使用
.clone
帮助我们克隆最后一行,并将其附加到末尾。小提琴: http://jsfiddle.net/garreh/w9fXJ/
并且只是为了抢先解决您的下一个问题,这里添加了删除行;-)
Fiddle: http://jsfiddle.net/garreh/w9fXJ/1/
Assuming you had a button id
add
and you wanted to add a table row onto the end:Using
.clone
to help us clone the last row, and appending it at the end.Fiddle: http://jsfiddle.net/garreh/w9fXJ/
And just to pre-empt you're next question, here's the addition of removing a row ;-)
Fiddle: http://jsfiddle.net/garreh/w9fXJ/1/
JQuery.前置 和 JQuery.append 会帮助你
JQuery.prepend and JQuery.append will help you