首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery Ajax保存页面显示/页面闪烁

问jQuery Ajax保存页面显示/页面闪烁
EN

Stack Overflow用户
提问于 2013-08-09 19:57:33
回答 1查看 170关注 0票数 0

我在jQuery中有以下Ajax代码,它使用save.php页面将数据从表单保存到MySQL数据库:

代码语言:javascript
复制
    $('#frmSurvey').append("<input type='hidden' name='page' value='"+$page+"' />"); // NOTE: this is done before the submit.

    document.frmSurvey.submit(); // There is some validation done and then the submit is called

    var url = "save.php"; // the script where you handle the form input.

    $.ajax({
           type: "POST",
           url: url,
           data: $("#frmSurvey").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });

以及HTML:

代码语言:javascript
复制
<form id="frmSurvey" name="frmSurvey" action="save.php" method="post" onsubmit="return false">

希望这一切都有意义。

我知道Ajax对save.php页面的调用意味着用户看不到页面,并且它发生在后台。然而,save.php页面仍然会出现在web地址栏中一两秒钟--这是正确的吗?有什么方法可以阻止这种情况发生吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-09 20:29:34

对另一个URL的导航是因为您实际上是在使用document.frmSurvey.submit();进行AJAX调用之前执行适当的表单提交--事实上,这将防止AJAX请求发生,因为导航到表单操作URL将停止当前页面中的JS处理。

如果删除该行,则表单将通过AJAX提交,无需任何重定向。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18154672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档