Javascript 将上一行数据添加到动态生成的行以存储数据
我复制这部分代码以动态添加行,效果非常好:Javascript 将上一行数据添加到动态生成的行以存储数据,javascript,php,jquery,html,codeigniter,Javascript,Php,Jquery,Html,Codeigniter,我复制这部分代码以动态添加行,效果非常好: <script> //define template var template = $('#sections .section:first').clone(); //define counter var sectionsCount = 1; //add new section $('body').on('click', '.addsection', function() { //increment sectionsCount
<script>
//define template
var template = $('#sections .section:first').clone();
//define counter
var sectionsCount = 1;
//add new section
$('body').on('click', '.addsection', function() {
//increment
sectionsCount++;
//loop through each input
var section = template.clone().find(':input').each(function(){
//set id to store the updated section number
var newId = this.id + sectionsCount;
//update for label
$(this).prev().attr('for', newId);
//update id - THIS CAUSES JqueryUI Datepicker to bug, also you shouldn't use numerical only IDs
//this.id = newId;
}).end()
//inject new section
.appendTo('#sections');
//initialize datePicker on last name=date[] element in HTML DOM
$("input[name='date[]']").last().datepicker();
return false;
});
//init original datePicker in HTML DOM
$("input[name='date[]']").last().datepicker();
//remove section
$('#sections').on('click', '.remove', function() {
//fade out section
$(this).parent().fadeOut(300, function(){
//remove parent element (main section)
$(this).parent().parent().empty();
return false;
});
return false;
});
</script>
//定义模板
var template=$('#sections.section:first').clone();
//定义计数器
var SectionScont=1;
//添加新节
$('body')。在('click','addsection',function()上{
//增量
sectionScont++;
//循环通过每个输入
var section=template.clone().find(':input').each(function(){
//设置id以存储更新的节号
var newId=this.id+sectionScont;
//标签更新
$(this.prev().attr('for',newId));
//更新id-这会导致JqueryUI Datepicker出现错误,而且您不应该只使用数字id
//this.id=newId;
})(完)
//注入新段
.附于(“#节”);
//在HTML DOM中的last name=date[]元素上初始化日期选择器
$(“输入[name='date[]']”).last().datepicker();
返回false;
});
//HTML DOM中的初始原始日期选择器
$(“输入[name='date[]']”).last().datepicker();
//删除部分
$('#sections')。在('单击','.remove',函数()上{
//淡出段
$(this.parent().fadeOut(300,function()){
//删除父元素(主部分)
$(this.parent().parent().empty();
返回false;
});
返回false;
});
是否可以修改此脚本以将以前的行数据添加到动态生成的行
现在,脚本仅创建具有空字段的新行,如此屏幕截图所示:
你能帮我解决这个问题吗
我使用codeigniter框架
非常感谢,riccardo您正在复制/克隆一个名为
template
的变量,该变量在顶部定义为第一部分。您可以在添加节时为其赋值,它可能是最后一个节
let sectionsCount = 1;
$('body').on('click', '.addsection', function() {
const template = $('#sections .section:last').clone();
sectionsCount++;
//loop through each input
template.find('input').each(function(){
//set id to store the updated section number
const newId = this.id + sectionsCount;
//update for label
$(this).prev().attr('for', newId);
//update id - THIS CAUSES JqueryUI Datepicker to bug, also you shouldn't use numerical only IDs
//this.id = newId;
});
//inject new section
template.appendTo('#sections');
//initialize datePicker on last name=date[] element in HTML DOM
$("input[name='date[]']").last().datepicker();
});
PS-我还没有测试过它。如果你能修改整个脚本,你能重写我吗?我的sript中有insert,但它不起作用创建一个JSFIDLE或类似的工具您可以创建一个JSFIDLE进行测试吗?