AJAX Form Submission in jQuery Mobile

前端 未结 3 1382
一向
一向 2021-01-02 22:38

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 paramet

相关标签:
3条回答
  • 2021-01-02 23:05

    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
        });
    });
    
    0 讨论(0)
  • 2021-01-02 23:09

    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">
    
    0 讨论(0)
  • 2021-01-02 23:22

    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="...">
    
    0 讨论(0)
提交回复
热议问题