我正在用 Jquery 动态创建一个表单,这个表单需要使用 AJAX 提交。我可能正在做一些愚蠢的事情,所以非常感谢你的帮助。
我在单击链接时动态创建表单:
$('.edit_item').click(function(){
$(this).closest('table').wrap("<form id='update_project' name='update_project' method='POST'>");
var input_name = $(this).closest('tr').find("td:eq(0)");
var input_submit = $(this).closest('tr').find("td:eq(1)");
input_name.html("<input type='text' id='update_name' name='update[]' value='"+input_name.text()+"' />");
input_submit.html("<input type='submit' value='update' id='update_submit' name='update_submit' />");
});
提交表单时:
$('#update_project').live("submit", function(e){
e.preventDefault();
$.post('project/update', $(this).serialize(), function(data){
$('#complete_msg').html(data);
});
$('.update_submit').css('background', '#c9c9c9');
});
很遗憾,页面正在刷新(不应该),没有返回。
谢谢!
最佳答案
我会将提交更改为按钮:
input_submit.html("<input type='button' value='update' id='update_submit' name='update_submit' />
并且事件 live 已折旧 ( http://api.jquery.com/live/ ):
$('#update_submit').click(function(e){
$.post('project/update', $('#update_project').serialize(), function(data){
$('#complete_msg').html(data);
});
$('.update_submit').css('background', '#c9c9c9');
});
关于javascript - 在 jQuery 中提交动态表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17432453/