单击按钮时添加表行
Adding Table Rows on Button Click
我想在用户单击Add button
时添加table rows
。在新行中有3个文本框。当添加行时,文本框的id
应设置为类似array list
。
例如
如果添加了第二行,则文本框ids
将为textbox1_1
textbox2_1
textbox3_1
如果添加了第3行,则文本框ids
将为textbox1_2
textbox2_2
textbox3_2
因为我想在最后将所有这些textboxes
值添加到一个single string
中。
小提琴
稍后添加:-事实上,表中第一次没有行。
试试这个:
$("#btnAdd").click(function(){
var id = $('table tr').size(),
i,
row = '<tr>';
for(i = 1; i <= 3; i++) {
row += '<td><input type="text" id="text'+i+'_'+id+'" />';
}
row += '</tr>';
$('table').append(row);
});
演示或者,如果你想从前面的行构建,你可以这样做:
$("#btnAdd").click(function(){
var id = $('table tr').size(),
tr = $('table tr').last().clone();
$(tr).find('td').each(function (index) {
$(this).attr('id', 'text'+index+'_'+id);
});
$('table').append(tr);
});
DEMO
试试这个:您可以通过克隆第一行来添加行,并通过迭代来更新每个input
的id。
//get the count of available row in table
var count = $('#dynamicTable tr').length;
$("#btnAdd").click(function(){
//clone the first row in table
var $tr = $('#dynamicTable tr:first').clone();
//iterate each input to change its id
$tr.find('input').each(function(){
var id = $(this).attr('id');
id = id.substring(0,id.length-1);
$(this).attr('id',id+count);
});
// update count
count++;
//add row to the table
$('#dynamicTable').append($tr);
});
JSFiddle链接
这样尝试
$("#btnAdd").click(function(){
var rowCount = $("table tr").length;
var firstRow = $("table tr:first").clone();
firstRow.find("input").each(function(){
this.id=this.id.replace("_0","_"+rowCount);
});
$("table").append(firstRow);
});
演示
用jquery少写多做。使用以下的链接
$("#btnAdd").click(function(){
$('table')
.find("tr:first") // traverse the first tr
.clone() // clone the row
.appendTo('table') // append the the table tag
.end() // reset to default selector
.find('input').each(function(i,x){
$(this).prop('id',this.id.split('_')[0]+'_'+(+$('table tr').length-1)); // assign the id to the added new row
});
});
编辑也使用FIDDLE完成希望有帮助。。。。
相关文章:
- 在HTML5画布上添加按钮和控件
- 无法在图像周围添加按钮
- Sitecore富文本编辑器-添加按钮
- 如何在标记href中添加按钮
- jqgridnavgrid在搜索按钮和自定义按钮之后添加按钮
- 如何在视频标签中添加按钮覆盖
- 添加按钮以切换浏览器
- 如何在php中添加按钮而不是错误状态和成功消息
- Struts2jquery可编辑网格,要禁用内联添加按钮
- 如何在服务器端的文本中添加按钮单击事件
- 简单的dart网页应用程序在IE上添加按钮的速度非常慢
- 向HTML页面添加按钮,更新表格信息
- (PHP&JavaScript)a href添加按钮赢得'我不适用于Mozilla,但适用于IE和谷歌
- 单击添加按钮后如何显示答案按钮
- 如何在 extjs 5 中的工具提示中添加按钮
- 在数据表的新行中添加按钮,变得未定义
- 如何向照片滑动添加按钮
- 动态添加按钮后刷新 jQm 标头
- PHP,MySql和JQuery - 按添加按钮后填充选择框
- 动态添加按钮,这些按钮也会动态添加文本框 AngularJS