大家好,我在网上找到了一个代码,我做了一些编辑,但我无法让它在同一页面上多次工作在这里你可以找到我的 jsfiddle:http: //jsfiddle.net/Scanu/DhpXN/
如果我重复 html 代码两次(corse)它不起作用我能做些什么来改进代码!?:/提前谢谢我的英语对不起我是意大利人
HTML
<div>
<input type="button" id="btnAdd" value="add another name" />
<input type="button" id="btnDel" value="remove name" />
</div>
<div class="clonedInput">
Link: <input type="text" name="link1" />
</div>
<br><br><br><br>
脚本
$('#btnAdd').click(function() {
var num = $(".clonedInput").length; // how many "duplicatable" input fields we currently have
var newNum = new Number(num + 1); // the numeric ID of the new input field being added
var newElem = $('.clonedInput:last').clone();
newElem.children(':first').attr('name', 'link' + newNum);
$('.clonedInput:last').after(newElem);
$('#btnDel').attr('disabled', false);
if (newNum == 5) $('#btnAdd').attr('disabled', 'disabled');
});
$('#btnDel').click(function() {
var num = $('.clonedInput').after().length;
$('.clonedInput:last').remove(); // remove the last element
$('#btnAdd').attr('disabled', false); // enable the "add" button
// if only one element remains, disable the "remove" button
if (num - 1 == 1) $('#btnDel').attr('disabled', 'disabled');
});
$('#btnDel').attr('disabled', 'disabled');