2012-01-31 48 views

回答

0
//bind event handler to the form for the `submit` event 
$('form').bind('submit', function() { 

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

    //show loading message 
    $.mobile.showLoadingMsg(); 

    //create an AJAX request using the form's attributes as settings 
    $.ajax({ 
     url  : $form.attr('action'), 
     type : $form.attr('method'), 
     data : $form.serialize(),//this serializes the form's data for transmission 
     success : function (serverResponse) { 

      //now hide the loading message because the AJAX call is done 
      $.mobile.hideLoadingMsg(); 

      //and redirect the user to the specified page, 
      //I am just forwarding the user to the value of the toggle 
      //but you can create an if/then statement that directs the user to the proper page 
      $.mobile.changePage($('#' + $form.find('.ui-slider-switch').val())); 
     } 
    }); 

    //return false to stop the default submission of the form 
    return false; 
}); 

您還需要禁用窗體的自動AJAX處理,你可以通過添加data-ajax="false"form標籤做到這一點:

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