jquery--form Asynchronous Commit

Source: Internet
Author: User

If no processing is done, the page is refreshed when the form commits, and in order to improve the experience, you can use jquery to submit the form asynchronously: through $ ("#form"). Serialize () Converts the data of a form element into a string and then passes the $.ajax () Executes an asynchronous request resource.

Page code:

<formMothod= "POST"ID= "Search_form">    <Divclass= "CF">       <labelclass= "Search-bar">          <inputID= "keyword"placeholder= "Please enter search keywords"name= "User_info_keyword"type= "text"value=""class= "Input-search">          <aID= "Search"class= "Btn-search"><Iclass= "Icon-search"></I></a>         <ahref= "javascript:;"class= "Btn-more"></a>      </label>    </Div></form>

jquery Code:

JQuery (document). Ready (function() {JQuery (' #search_form '). Bind ("Submit",function(){         varKey_word = JQuery ("#keyword"). Val (); if(Key_word = = "") {return false; } varOptions ={URL:'/portrait/user_info_display?user_info_keyword= ' +Key_word, type:' Post ', DataType:' Text ', Data: $ ("#search_form"). Serialize (), Success:function(data) {if(Data.length > 0) JQuery ("#user_info"). HTML (data);         }         };         $.ajax (options); return false; })   $(' #search '). Click (function(){        $(' #search_form '). Submit (); })});

The code completes two functions: 1. Press ENTER after entering a keyword, a POST request is sent to the server, the form is submitted asynchronously, and a portion of the page is refreshed; 2. After you enter a keyword, click the query button, or you can refresh some of the pages asynchronously.

Note that a POST request is sent after the form is submitted, and the Click button sends a GET request, so we can use jquery to make the button click on the post to submit, so the backend does not have to write the code to process the GET request.

jquery--form Asynchronous Commit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.