需要动态地向表添加/删除行,我使用以下jquery向表中添加/删除行: 在这里,我解决了更改输入标记的name属性的问题。 但是在添加动态行的同一个表中获取发布数据结果(同时服务器端验证)时,我遇到了问题。 有没有办法将动态php脚本设置为输入标记的value属性? 另外,如何在帖子刷新后持续动态添加行?
(文档)$。就绪(函数(){ //此行克隆'.row'类中的行并将其转换为纯HTML。 var clonedRow = $('。row')。clone()。html();
//This line wraps the clonedRow and wraps it <tr> tags since cloning ignores those tags
var appendRow = '<tr class = "row">' + clonedRow + '</tr>';
//$('#btnAddMore').click(function(){
//this line get's the last row and appends the appendRow when it finds the correct row.
// $('.employmentHistoryForm tr:last').after(appendRow);
//});
$("#btnAddMore").click(function() {
$(".employmentHistoryForm tr:last")
.clone()
.appendTo(".employmentHistoryForm")
.find(':input')
.attr('name', function(index, name) {
return name.replace(/(\d+)$/, function(fullMatch, n) {
return Number(n) + 1;
});
})
});
//when you click on the button called "delete", the function inside will be triggered.
$('.deleteThisRow').live('click',function(){
var rowLength = $('.row').length;
//this line makes sure that we don't ever run out of rows.
if(rowLength > 1){
deleteRow(this);
}else{
$('.employmentHistoryForm tr:last').after(appendRow);
deleteRow(this);
}
});
function deleteRow(currentNode){
$(currentNode).parent().parent().remove();
}
});