在 Jquery 中将事件应用于动态添加表单时遇到一些问题
我通过 jQuery 在对话框中加载表单
代码就像
<form class ="form1" action="" method="post" enctype="multipart/form-data" >
...
</form>
我使用 jQuery 表单插件来提交这样的表单
$(".form1").live('submit', function(e) {
var options = {
target: '.ajaxMessage',
beforeSubmit: showRequest,
success: showResponse,
type: 'POST'
};
alert('test');
$(this).ajaxSubmit(options);
return false;
});
现在
如果我直接加载表单而不使用 AJAX,然后提交表单,则表单将成功提交,无需任何操作问题。十次中的十次有效
在第二种情况下,我动态加载表单。当我单击表单链接时,我会在 jquery 对话框中动态加载表单,然后如果我单击提交表单,我可以看到警报,但表单未提交。但有时有效,有时无效。我想说它十有八九会工作两次。
控制台也没有显示任何错误
有什么办法我可以找到问题所在
I am loading the form in dialog box via jQuery
The code is like
<form class ="form1" action="" method="post" enctype="multipart/form-data" >
...
</form>
I am using jQuery form plugin to submit form like this
$(".form1").live('submit', function(e) {
var options = {
target: '.ajaxMessage',
beforeSubmit: showRequest,
success: showResponse,
type: 'POST'
};
alert('test');
$(this).ajaxSubmit(options);
return false;
});
Now
If i load the form directly without AJAX and then i submit the form then form gets submiited successfuly without any problem. It works 10 out of 10 times
In second case I load the form dynamically. When i click on form link then i load the form dynamically in a jquery dialog box then if i click on submit form then i can see the alert but form is not submitted. But it works sometimes but sometimes not. I would say it work 2 times out of 10.
Firebug console is also not showing any error
Is there any way i can find whats problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

Firebug 通常(我实际上认为根本不会)不会显示 ajax 调用的任何错误,而是错误将出现在 ajax 请求中(仍在 Firebug 中)。单击请求,然后单击响应。
我的猜测是,您发送的参数有问题,或者您返回的内容有问题(即,当 ajax 期望 json 时返回 html,这将导致成功永远不会被触发)
此外,尝试传递
error:function(jqXHR, textStatus, errorThrown){}
到 ` ajaxSubmit 参数,看看会发生什么。Firebug will usually (I actually think not at all) won't show any errors for a ajax call, instead the error will be in the ajax request(still in firebug). Click the request and then response.
My guess is that there is a problem with the params you are sending or there is something wrong with what you a returning(i.e. you return html when ajax is expecting json, this will cause success never to be fired)
Also, try to pass an
error:function(jqXHR, textStatus, errorThrown){}
to the ` ajaxSubmit params and see what happens.