2017-06-11 42 views
0

我這樣的代碼:點擊按鈕時如何清除表單?

$('body').on('click', '#add-image', function(e) { 
    $(this).find('#form-add').trigger('reset'); 
    // $(this).find('#form-add').clearForm(); 
    // $('#form-add').clearForm(); 
    // $('#form-add').val(""); 
    // ... 
}); 

如果按鈕點擊,我想形式來明確

我嘗試這樣,但它不工作

我怎樣才能解決這個問題?

+0

試試$('#form-add')。trigger('reset'),參見https://stackoverflow.com/questions/3786694/how-to-reset-clear-form-through-javascript – Zoltan

回答

2

使用Javascript或JQuery清除整個表單,建議您明確清除每個元素。

例如,清除所有的文本輸入:

$('#txtbox1').val(''); 
$('#txtbox2').val(''); 
etc. etc. 

另一個不太推薦替代方法是使用reset()

$('#myForm')[0].reset(); 

但這並不保證100%清除中的所有元素中,由於一些元素可能不在您的表單中。

+0

我需要你幫幫我。看看這個:https://stackoverflow.com/questions/44486251/why-image-not-display-in-modal-bootstrap –

0

讓我們假設你有一個類clearAll一個按鈕,調用下面的jQuery的功能,這將清除所有的文本框和textarea的一次......更容易和簡單的...但是你想定製...

$(".clearAll").click(function() { 
    $(this).closest('form').find("input[type=text], textarea").val(""); 
}); 
0

empty()功能jQuery的嘗試

$('body').on('click', '#add-image', function(e) { 
    $(this).find('#form-add').trigger('reset'); 
    $(this).find('#form-add').find('input,textarea').empty() 
}); 
+0

我需要你的幫助。看看這個:https://stackoverflow.com/questions/44486251/why-image-not-display-in-modal-bootstrap –

0

您可以清除該undet的所有文字惹人這form這樣的:

$("#clean").click(function() { 
    $(this) 
    .closest('form') 
    .find("input[type=text],textarea") 
    .val(''); 
}); 
相關問題