0
如何將此代碼轉換爲一個函數,我需要將其綁定到div ID並傳入name
和id
參數以用於輸入?從代碼塊創建jQuery函數
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);
});
'var newfun = $(function myfun(name,id){... your code});'' – SRy 2013-03-08 17:16:45