Javascript jquery克隆表单单个字段和增量id
我有一个单独的文件,我需要使用“克隆”按钮和“删除”按钮克隆该文件以仅删除克隆的文件 我制作了这个简单的脚本,但我相信它包含了一些错误,因为它不起作用:) HTMLJavascript jquery克隆表单单个字段和增量id,javascript,jquery,Javascript,Jquery,我有一个单独的文件,我需要使用“克隆”按钮和“删除”按钮克隆该文件以仅删除克隆的文件 我制作了这个简单的脚本,但我相信它包含了一些错误,因为它不起作用:) HTML <form method="post"> <div id="fileds"> <select name="lang" id='lang'> <option>select language</option> </select>
<form method="post">
<div id="fileds">
<select name="lang" id='lang'>
<option>select language</option>
</select>
</div>
</form>
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
此外,我也没有找到如何编写删除按钮的删除代码
谢谢1)#lang
不是.clone
2) .fields
应该是#field
,因为它是和ID一样的
这个代码应该可以工作。生活
lang
不是按钮的父级,我为remove button添加了一个类和一个处理程序,请尝试以下操作:
$(function(){
$(".clone").on("click", function() { // you can use `on` instead of live which is deprecated
var cloneIndex = $(".lang").length; // you can find the the current length of `.lang` within the handler
$(".lang:last").clone() // clones the last element with class of `lang`
.attr("id", "lang" + cloneIndex)
.appendTo("#fileds") // change this to id selector
});
$("button.remove").on("click", function(){ // '.remove' click handler
$(".lang:last").remove()
})
});
#lang
不是的父项。克隆按钮在示例中起作用,但当我在页面中使用它时,它不起作用。它可以工作,但它也可以删除源文件。我不想删除原始文件
$(function() {
var counter = 1;
$(".clone").live("click", function() {
$("#lang:first").clone().appendTo("#fileds").addClass("lang" + counter);
counter++
});
$(".remove").live('click', function() {
if (counter > 1) { //Only apply if the lang field is more than 1
counter = counter - 1;
$("#lang:last").remove();
}
});
});
$(function(){
$(".clone").on("click", function() { // you can use `on` instead of live which is deprecated
var cloneIndex = $(".lang").length; // you can find the the current length of `.lang` within the handler
$(".lang:last").clone() // clones the last element with class of `lang`
.attr("id", "lang" + cloneIndex)
.appendTo("#fileds") // change this to id selector
});
$("button.remove").on("click", function(){ // '.remove' click handler
$(".lang:last").remove()
})
});