提交AJAX表单时jQuery意外刷新页面 [英] jQuery unexpected refresh of page when submit an AJAX form

查看:131
本文介绍了提交AJAX表单时jQuery意外刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AJAX表单提交方面遇到问题.

I have a problem, with AJAX submit of forms.

我有一段这样的代码:

$("#content").html("<form id='requestInfo'>............</form");

将创建表单的代码与 id = content 放入div.

That put in a div with id=content the piece of code that creates a form.

我还有另一段代码:

$("#requestInfo").submit(function( event ) {
  event.preventDefault();
  ...code...
});

我希望在commit()方法中执行代码,在该方法中我可以使用该格式的数据来处理我的事情,但是我却刷新了页面.

I would expect an execution of the code inside the submit() method where I can do my things with data of that form, but I get a refresh of the page, instead.

有人知道这可能是什么问题吗?它应该与所有浏览器兼容.

Does anyone know what could be the problem? It should be compatible with all browsers.

谢谢!

推荐答案

您正在动态创建表单,因此标准事件绑定将不起作用.您需要使用事件委托

You are creating your form dynamically thus standard event binding will not work. You need to use event delegation

$("#content").on('submit', '#requestInfo', function(evt) {
    evt.preventDefault();
});

这篇关于提交AJAX表单时jQuery意外刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆