This is something I accidentally saw today. I first came into contact with jquery and wrote it as an unexpected result for me. It's just a beginner's understanding...
Start writing this verification from understanding jquery...
In most cases, writing jquery code requires us to put jquery code in any of the following three functions.
There are three writing methods, which have the same effect, a bit like Window. onload, but there are also differences, that is, window. onload is executed only after the page is loaded, and $ (document ). the ready () method only performs operations after all labels are loaded. There is also $ (document ). ready () can be called multiple times, while window. onload is not allowed and will be overwritten by the next call...
First: $ (document). ready (function (){.....});
Type 2: $ (). ready (function (){......});
Third: $ (function (){......});
Note: use $ (document) in jQuery ). the event handler registered by the ready () method can be called as long as the DOM is fully ready. For example, if the label of an image is complete, you do not need to wait until the image is loaded, you can set attributes or styles of an image.
How does ajax work in jquery?
Ajax transfers values in two ways: get/post
[Get method]
Var changeUrl = "CheckUser. php? UName = "+ name; // jump to the judgment page
$. Get (changeUrl, function (result) {// run ajax to enter the url page and return result
// Return the result to output the description.
} Www.2cto.com
[Post method]
Var username = $ (this). val ();
Var changeUrl = "CheckUser. php ";
$. Post (changeUrl, {name: username}, function (result ){...}
Go to the CheckUser. php page, receive the Code through get/post, and check whether the code exists in the Database. Then, return a value...
Html code: User name: <input type = "text" id = "userName"/> <span id = "Info"> </span>
Jquery code: $ ("# userName"). blur (function (){
Var username = $ (this). val ();
Var changeUrl = "CheckUser. php"; // jump to the judgment page
// The following shows how to run ajax, enter the changeUrl page, input name, and return result
$. Post (changeUrl, {name: username}, function (result ){
If (result = '1 '){
$ ("# Info" ).html ("<font color = \" red \ "> the user name exists! </Font> ");
} Else {
$ ("# Info" ).html ("<font color = \" green \ "> registered! </Font> ");
}}})
CheckUser. php
<? Php
If ($ _ POST ['name']) {// receives the value passed by post.
$ Username = trim ($ _ POST ['name']);
// Make a judgment. It was supposed to be retrieved from the database. I'm just lazy, just testing...
If ($ username = "admin "){
Echo '1 ';
} Else {
Echo '0 ';
}}
?>
I usually read a lot, but I don't know how to write it. This may be because this is an accident, so I will write it without thinking about it. I am afraid that the accident will disappear .. Haha...
This article is from the "Heng" blog