Using JQuery to submit Form with no refresh is accompanied by an official demonstration

Source: Internet
Author: User

Use JQuery to submit the form without refreshing the following code

<form id= "Formupdate" action= "/updateinfo?fun=update" method= "post" target= "_self" >

<input id= "Id_user" name= "Id_user" type= "hidden" value= "0001" >

<input name= "button" type= "Submit" value= "OK" onclick= "submittheform ()"/>

</form>

<script lang= "JScript" >
function Submittheform ()
{

Get form by ID of form
var $form = $ (' #formUpdate '); Is critical

Get the form's submit path
url = $form. attr (' action '); Get the submit path to the form and assign it to the URL

Submitted as post, callback function functions return data
var i = $.post (URL, $form. Serialize (), function (data)
{

Display return information
alert (data);
});

Another form jquery.post (URL, $form. Serialize ()); can also
}
</script>

JQuery Official website http://jquery.com/ Examples: example:request the test.php page, but ignore the return results.

$.post ("test.php");
example:request the test.php page and send some additional data along (while still ignoring the return results).
$.post ("test.php", {name: "John", Time: "2pm"});
example:pass Arrays of data to the server (while still ignoring the return results).
$.post ("test.php", {' choices[] ': ["Jon", "Susan"]});
example:send form data using AJAX requests
$.post ("test.php", $ ("#testform"). Serialize ());
Example:alert out of the results from requesting test.php (HTML or XML, depending on what is returned).
$.post ("test.php", function (data) {
alert ("Data Loaded:" + data);
});
Example:alert out the results from requesting test.php with a additional payload of data (HTML or XML, depending on What was returned).
$.post ("test.php", {name: "John", Time: "2pm"},
function (data) {
alert ("Data Loaded:" + data);
});
example:gets The test.php page content, store it in a Xmlhttpresponse object and applies the process () JavaScript fun Ction.
$.post ("test.php", {name: "John", Time: "2pm"},
function (data) {
process (data);
},
"xml" 
  
   );
  
example:posts to the test.php page and gets contents which has been in JSON format (returned Echo <?php De (Array ("name" => "John", "Time" => "2pm")),?>).
$.post ("test.php", {"Func": "Getnameandtime"},
function (data) {
console.log (data.name);//John
con Sole.log (Data.time); 2pm
}, "json");
example:post a form using AJAX and put results in a div <! DOCTYPE html>
<script src= "Http://code.jquery.com/jquery-latest.js" ></script>
<body>
<form action= "/" id= "Searchform" >
<input type= "text" name= "s" placeholder= "Search ..."/>
<input type= "Submit" value= "Search"/>
</form>
<!--The result of the search is rendered inside this div-->
<div id= "Result" ></div>

<script>
/* Attach a submit handler to the form * * *
$ ("#searchForm"). Submit (Function (event) {

/* Stop form from submitting normally * *
Event.preventdefault ();

/* Get some values from elements on the page: * *
var $form = $ (this),
term = $form. Find (' Input[name= "s"] "). Val (),
url = $form. attr (' action ');

/* Send the data using post and put the results in a div * *
$.post (URL, {s:term}, function (data)
{
var content = $ (data). Find (' #content ');
$ ("#result"). Empty (). append (content);
}
);
});
</script>

</body>

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.