我有一個需要添加和刪除文本框的表單。如何刪除jQuery中動態創建的內容
我得到了添加文本框部分工作。當你點擊加號按鈕時,新的文本框被添加 - 沒有問題。
但是,當我單擊垃圾桶按鈕時,我無法獲取要刪除的文本框條目。
有什麼想法?
這裏的小提琴 - >https://jsfiddle.net/u4h9ohv6/2/
$('.fcClick').on('click', function(event) {
if($(this).attr('name') == 'fcButtonMinus') {
$(this).closest('.input-group').find('input').val('');
//$(this).closest("div.input-group").addClass("hideContentBlock");
return false;
}
if($(this).attr('name') == 'fcButtonPlus') {
$('span#fcAdds').append(
'<div class="col-md-10 persStmtPad input-group" style="margin-top:0.125em;margin-left:35px">' +
'<input id="fallCourses" name="fallCourses[]" type="text" placeholder="additional Fall Course" class="form-control input-md">' +
'<div class="input-group-btn">' +
'<button name="fcButtonMinus" class="btn btn-default btn-md" onClick="fcFunc()" style="background-color:#efefef;color:#999;margin-top:12px" title="Delete this Fall Course"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>' +
'</div>' +
'</div>'
);
return false;
}
});
function fcFunc() {
$(this).closest("div.input-group").addClass("hideContentBlock");
return false;
}
另外,如果您有關於如何防止提交每次點擊的形式任何想法,這將是真棒。現在,即使我有return false
,每次點擊都會提交表單。
這裏是我努力使工作代碼:
function fcFunc() {
$(this).closest('.input-group').find('input').val('');
$(this).closest("div.input-group").addClass("hideContentBlock");
//$(this).closest("div.input-group").focus(); // did not stop form submit
$(this).submit(function(event) {
event.preventDefault(); // does not stop form from being submitted
});
return alert('interception'); // this works
return false; // this too does not do the trick -- the form still tries to submit
}
您需要將綁定更改爲類似'$( '#集裝箱')。在( '點擊', '.fnClick',函數(){...}'並嵌套在一個'#容器'內的所有東西。 – nurdyguy
http://api.jquery.com/on/ – nurdyguy