我怎样才能把这段代码变成一个函数,我需要做的就是将它绑定到一个 div id,并为输入传递参数name
和id
参数?
var startingNo = 3;
var $node = "";
for(varCount=0;varCount<=startingNo;varCount++){
var displayCount = varCount+1;
$node += '<p><input type="text" name="duties'+displayCount+'" id="duties'+displayCount+'"><span class="removeVar">Remove Variable</span></p>';
}
//add them to the DOM
$('#duties').prepend($node);
//remove a textfield
$('#duties').on('click', '.removeVar', function(){
$(this).parent().remove();
varCount--;
});
//add a new node
$('#addVar').on('click', function(){
varCount++;
$node = '<p><input type="text" name="duties'+varCount+'" id="duties'+varCount+'"><span class="removeVar">Remove Variable</span></p>';
$(this).parent().before($node);
});