由于需要向表中动态添加/删除行,我使用以下 jquery 向表中添加/删除行:这里我解决了更改输入标签的名称属性的问题。但是我在添加动态行时在同一个表中获取发布数据结果(同时服务器端验证)时遇到问题。有没有办法将动态 php 脚本设置为输入标签的值属性?另外,如何在刷新后保持动态添加的行?
$(document).ready(function(){ //这一行克隆了 '.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();
}
});