大约一年前,我创建了一个使用克隆复制表单的脚本(在 StackOverflow 的帮助下)。该脚本完成了它需要做的事情,但是在使用了一段时间后,我发现了一些应该有所不同的东西。问题是,由于我对 javascript 和 jQuery 的了解非常有限,这似乎太难了。
剧本:
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$('.startdatum').removeClass('hasDatepicker').datepicker({
dateFormat: 'dd-mm-yy',
constrainInput: false
});
$("input[type='button'].AddRow").live('click',
function() {
var index = $(this).closest('div').index();
if (index > 0) {
$(this).closest('div').remove();
} else {
var $tr = $(this).closest('div').clone(true);
$tr.find('input.AddRow').val("Delete");
var $input = $tr.find('input.startdatum');
var index = $('input#counter').val();
var id = 'datepicker' + index;
index++;
$('input#counter').val(index);
$input.attr('id', id).data('index', index);
//console.log(index);
$(this).closest('span').append($tr);
$('.startdatum').each(function() {
$(this).datepicker('destroy');
$(this).datepicker({
dateFormat: 'dd-mm-yy',
constrainInput: false
});
});
}
});
});//]]>
</script>
问题
该脚本现在克隆表单,包括所有填写的字段。我读了http://api.jquery.com/clone/但我不知道如何复制空字段。
初始表单有一个表单按钮来添加另一个表单。每个克隆的表单都有一个用于删除特定表单的按钮。我想在每一种形式中都有(除了初始形式中的删除,应该保持原样)。
最重要的是,我正在使用脚本来检查所有字段是否均已填写且有效。在填写所有字段之前,不应出现克隆按钮。
$(文档).ready(函数() {
// ETC。// Wanneer het formulier verstuurd wordt $("#theform").submit(function(){ isValid = true; // Valideer Aanhef var aanhef = $('input[name=aanhef]:checked').val(); if(aanhef != 'v' && aanhef != 'm') { isValid = false; $('#msg_aanhef').html('Vul uw aanhef in.').show(); $('#aanhef').addClass("fout"); } else { $('#msg_aanhef').html('').hide(); }