我正尝试创建上传表单。到目前为止它运行良好,我正在尝试解决一些我不喜欢的错误。克隆表单输入和清除值
行,我似乎有麻烦是
$(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删除,我想这个留下。
借来是否有递增具体要求'id'属性上的每个克隆?我只问,因为克隆,附加和清除值可以用很少的代码来执行。 – andyb