2014-02-28 118 views
3

我正尝试创建上传表单。到目前为止它运行良好,我正在尝试解决一些我不喜欢的错误。克隆表单输入和清除值

行,我似乎有麻烦是

$(element).find(">:first-child").attr("value", ""); 

当克隆形式,它克隆div和替换没有留下一个空白表单的价值,这个效果很好,如果我删除该行我会得到以前的窗体的值,所以它会很好的空白窗体显示。

我遇到的问题是当你删除一个窗体的所有窗体的值删除,我想要的是当你删除一个窗体时,为其他窗体保留单独的值。

这里有一个小提琴http://jsfiddle.net/d77pd/1/或见下文

HTML代码

<button class="clone">Add an Image</button> 
<div id="upload_image_sets"> 
    <div id="clonedInput1" class="clonedInput"> 
     <input type="text" id="upload_image_link_1" class="image" size="36" name="hero_options[upload_image_link_1]" value="' . $hero_options['upload_image_link_1'] . '" /> 
     <input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" /> 
     <button class="remove">Remove</button> 
    </div> 
</div> 

的JavaScript

function updateClonedInput(index, element) { 
    $(element).appendTo("#upload_image_sets").attr("id", "clonedInput" + index); 
    $(element).find(">:first-child").attr("id", "cs_product_menu_img_src_" + index); 
    $(element).find(">:first-child").attr("name", "hero_options[upload_image_link_" + index + "]"); 
    $(element).find(">:first-child").attr("value", ""); 
    $(element).find(">:first-child").next().attr("id", "cs_product_menu_img_src_" + index + "_button"); 
    displayRemove(); 
} 

function displayRemove() { 
    if ($('.clonedInput').length === 1) { 
     $('.remove').hide(); 
    } else { 
     $('.remove').show(); 
    } 
} 
displayRemove(); 

$(document).on("click", ".clone", function (e) { 
    e.preventDefault(); 
    var cloneIndex = $(".clonedInput").length + 1; 
    var new_Input = $(this).closest('.clonedInput').length ? $(this).closest('.clonedInput').clone() : $(".clonedInput:last").clone(); 
    updateClonedInput(cloneIndex, new_Input); 
}); 
$(document).on("click", ".remove", function (e) { 
    e.preventDefault(); 
    $(this).parents(".clonedInput").remove(); 
    $(".clonedInput").each(function (cloneIndex, clonedElement) { 
     updateClonedInput(cloneIndex + 1, clonedElement); 
    }) 
}); 

克隆形式几次,如果你删除任何形式的分开形式第一个与内容,你会注意到第一个表格的内容nt删除,我想这个留下。

+0

借来是否有递增具体要求'id'属性上的每个克隆?我只问,因为克隆,附加和清除值可以用很少的代码来执行。 – andyb

回答

0

第一种方法: 从增加功能调用updateClonedInput(cloneIndex, new_Input);后调用$(element).find(">:first-child").attr("value", "");

Working Demo First approach:

方法二:

我已经修改一些代码。传递函数updateClonedInput多了一个布尔参数添加时。其中将被设置为true,并设置为false时,DOM是removed.This将阻止值上删除功能得到更换:

function updateClonedInput(index, element,param) { 
    $(element).appendTo("#upload_image_sets").attr("id", "clonedInput" + index); 
    $(element).find(">:first-child").attr("id", "cs_product_menu_img_src_" + index); 
    $(element).find(">:first-child").attr("name", "hero_options[upload_image_link_" + index + "]"); 
if(param) 
    $(element).find(">:first-child").attr("value", ""); 
    $(element).find(">:first-child").next().attr("id", "cs_product_menu_img_src_" + index + "_button"); 
    displayRemove(); 
} 

function displayRemove() { 
if($('.clonedInput').length === 1) { 
    $('.remove').hide(); 
} else { 
    $('.remove').show(); 
} 
} 
displayRemove(); 

$(document).on("click", ".clone", function(e){ 
    e.preventDefault(); 
    var cloneIndex = $(".clonedInput").length + 1; 
    var new_Input = $(this).closest('.clonedInput').length ? $(this).closest('.clonedInput').clone() : $(".clonedInput:last").clone(); 
    updateClonedInput(cloneIndex, new_Input,true);  
}); 
$(document).on("click", ".remove", function(e){ 
    e.preventDefault(); 
    $(this).parents(".clonedInput").remove(); 
    $(".clonedInput").each(function (cloneIndex, clonedElement) { 
     updateClonedInput(cloneIndex + 1, clonedElement,false); 
    }) 
}); 

Working Demo Second Approach

+0

请解释您所做的更改以及原因。 – andyb

+0

这两个演示都不适合我。添加到克隆行的值被复制到下一个克隆。 – andyb

0

的备用解决方案从第一个元素创建一个空白克隆一次,然后在每次需要新行时使用它。它也使用CSS来隐藏/显示删除按钮基于您只需要删除按钮,除非它是唯一的孩子。

声明:我删除了id操作,因为我不确定您是否真的需要它。如有必要,我可以更新。

Demo

HTML

<button class="clone">Add an Image</button> 
<div id="upload_image_sets"> 
    <div class="clonedInput"> 
     <input type="text" class="image" size="36" name="hero_options[upload_image_link_1]" value="an initial value" /> 
     <input class="button upload_images" type="button" value="Upload Image" /> 
     <button class="remove">Remove</button> 
    </div> 
</div> 

CSS

.clonedInput .remove { 
    display:inline-block; 
} 

.clonedInput:only-child .remove { 
    display:none; 
} 

的JavaScript

function resetForm($form) { 
    $form.find('input:text, input:password, input:file, select, textarea').val(''); 
    $form.find('input:radio, input:checkbox').removeAttr('checked').removeAttr('selected'); 
} 

var $blankClone = $('.clonedInput').clone(); 
resetForm($blankClone); 

$(document).on('click', '.clone', function(e) { 
    e.preventDefault(); 
    $blankClone.clone().appendTo('#upload_image_sets'); 
}); 

$('#upload_image_sets').on('click', '.remove', function(e) { 
    e.preventDefault(); 
    $(this).closest('.clonedInput').remove(); 
}); 

resetForm()Resetting a multi-stage form with jQuery

+0

甚至删除按钮不会附加到每个添加。 –

+0

是的,发现并已更新。谢谢。 – andyb