jQuery Mobile 中的 AJAX 表单提交

发布于 2024-12-13 20:59:19 字数 458 浏览 3 评论 0原文

我正在尝试通过 ajax 在 jQuery Mobile 网站上提交一个简单的登录表单,但遇到了麻烦。

似乎当我提交表单(通过 POST)时,表单参数被添加到 url 中。不仅如此,他们还删除了我在提交表单之前所在的锚定页面。

例如,我在页面 localhost:8080/myapp/#sign_up

然后我提交表单,导致 url 变为: localhost:8080/myapp/[电子邮件受保护]&pass=pass

因此,如果我遇到验证错误并单击“后退”按钮,我不会返回到 <代码>#sign_up 页面。

有什么想法吗?

I'm trying to submit a simple login form via ajax on a jQuery Mobile site but I'm having trouble.

It seems that when I submit the form (via POST), the form parameters are getting added to the url. Not only that, they erase the anchored page I was at before form submission.

For example, I'm on page localhost:8080/myapp/#sign_up

Then I submit the form causing the url to become: localhost:8080/myapp/[email protected]&pass=pass

So if I hit validation errors and click a 'back' button, I don't get returned back to the #sign_up page.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌面超妹 2024-12-20 20:59:19

如果您使用自定义 submit 事件处理程序处理表单提交,则可以在同一页面上处理验证:

//bind an event handler to the submit event for your login form
$(document).on('submit', '#form_id', function (e) {

    //cache the form element for use in this function
    var $this = $(this);

    //prevent the default submission of the form
    e.preventDefault();

    //run an AJAX post request to your server-side script, $this.serialize() is the data from your form being added to the request
    $.post($this.attr('action'), $this.serialize(), function (responseData) {

        //in here you can analyze the output from your server-side script (responseData) and validate the user's login without leaving the page
    });
});

要阻止 jQuery Mobile 运行其自己的表单 AJAX 提交,请将其放在表单标记上:

<form data-ajax="false" action="...">

If you handle form submission with a custom submit event handler you can handle validation on the same page:

//bind an event handler to the submit event for your login form
$(document).on('submit', '#form_id', function (e) {

    //cache the form element for use in this function
    var $this = $(this);

    //prevent the default submission of the form
    e.preventDefault();

    //run an AJAX post request to your server-side script, $this.serialize() is the data from your form being added to the request
    $.post($this.attr('action'), $this.serialize(), function (responseData) {

        //in here you can analyze the output from your server-side script (responseData) and validate the user's login without leaving the page
    });
});

To stop jQuery Mobile from running its own AJAX sumbission of your form put this on your form tag:

<form data-ajax="false" action="...">
甜宝宝 2024-12-20 20:59:19

上面的 Jaspers 解决方案对我有用!我唯一需要调整的是将 .live 替换为 .submit (.live 现已弃用)。所以现在是这样的:

$('#form_id').submit(function (e) {

    //cache the form element for use in this function
    var $this = $(this);

    //prevent the default submission of the form
    e.preventDefault();

    //run an AJAX post request to your server-side script, $this.serialize() is the data from your form being added to the request
    $.post($this.attr('action'), $this.serialize(), function (responseData) {

        //in here you can analyze the output from your server-side script (responseData) and validate the user's login without leaving the page
    });
});

Jaspers solution above worked for me! The only thing I had to adjust was replacing .live with .submit (.live is now deprecated). So now its like this:

$('#form_id').submit(function (e) {

    //cache the form element for use in this function
    var $this = $(this);

    //prevent the default submission of the form
    e.preventDefault();

    //run an AJAX post request to your server-side script, $this.serialize() is the data from your form being added to the request
    $.post($this.attr('action'), $this.serialize(), function (responseData) {

        //in here you can analyze the output from your server-side script (responseData) and validate the user's login without leaving the page
    });
});
故人的歌 2024-12-20 20:59:19

如果您想提交表单而不使用 ajax(默认),则必须将 'data-ajax="false"' 添加到表单字符串中:

 <form data-ajax="false" action="test.php" method="POST">

If you want to submit a form and not use ajax (which is default) you must add 'data-ajax="false"' to your form string:

 <form data-ajax="false" action="test.php" method="POST">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文