有人可以告訴我如何獲取輸入值並在用戶點擊添加鏈接後將其附加到div上?將輸入值附加到div
這是我能做的最好的。 HTML:
<div id="customUtility-container"></div>
<a href="#" id="addUtility">Add</a>
的jQuery:
$(function() {
var addDiv = $('#customUtility-container');
var i = $('#customUtility-container').size() + 1;
$('#addUtility').live('click', function() {
$('#customUtility').val().appendTo(addDiv);
$('<p><label for="customUtility-container"><input type="text" id="customUtility" size="20" name="customUtility_' + i +'" value="" placeholder="" /></label> <a href="#" id="removeUtility">Remove</a></p>').appendTo(addDiv);
i++;
return false;
});
$('#removeUtility').live('click', function() {
if(i > 2) {
$(this).parents('p').remove();
i--;
}
return false;
});
然而,這創建了另一個輸入字段;我只想要一個輸入框,讓用戶單擊添加,然後它將該值,放入列表中,並清除輸入框,以便用戶可以再次添加其他內容。