我有2個函數,第一個調用表單,第二個通過ajax提交該表單。 Hovewer我無法將提交事件綁定到新創建的表單,爲什麼這樣呢?將事件綁定到動態生成的表單
獲取形式
$("#discount").click(function(){
$.ajax({url:"index.php?module=products&view=addajax",success:function(result){
$(".forma").html(result);
}});
});
通過Ajax
$('#my_form').on('submit', (function(evnt){
evnt.preventDefault(); //Avoid that the event 'submit' continues with its normal execution, so that, we avoid to reload the whole page
data = $("form#my_form").serialize();
$.post('index.php/products/adds',
$("form#my_form").serialize(), //Serialize all the content of our form to URL format
function (data) {
$('div#sending_form').prepend(data); //Add the AJAX response to some div that is going to show the message
})
}));