Examples of dom operations and events in jquery-Form Verification

Source: Internet
Author: User

Obviously, this can improve the user experience.
Html code:
Copy codeThe Code is as follows:
<Form method = "post" action = "">
<Div class = "int">
<Label for = "username"> User name: </label>
<Input type = "text" id = "username" class = "required"/>
</Div> </form>

Jquery code:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Function (){
$ ('Form: input'). blur (function (){
Var $ parent = $ (this). parent ();
$ Parent. find (". formtips"). remove ();
If ($ (this). is ('# username ')){
If (this. value = "" | this. value. length <6 ){
Var msg = "<span class = 'formtips error'> enter at least 6 usernames </span> ";
$ (Msg). appendTo ($ parent );
} Else {
Var msg = "<span class = 'formtips success '> the input is correct </span> ";
$ (Msg). appendTo ($ parent );
}
}
}). Keyup (function (){
$ (This). triggerHandler ("blur ");
}). Focus (function (){
$ (This). triggerHandler ("blur ");
})
})
</Script>

Now, let's analyze the jquery statements in detail.
First look at the dom operation statement
$ ('Form: input') is used to find the <input>, <textarea>, <select>, and <button> elements under the form element.
Similarly, there are $ (': text'), $ (': checkbox'), and so on. Anyway, you only need to understand that the corresponding elements can be obtained through the form selector only in the form.
Parent () is the parent node that finds matching elements. Find () is used to search for elements matching expressions. Remove () is used to delete elements.
Is () is to use an expression to check the Element Set of the current selector. if at least one matching element exists, true is returned.
AppendTo () is to add an element to an element.
TriggerHandler () is a special method that triggers a specific event on the element.
Let's look at the event statement.
Keyup () is triggered when the button is up.
It is not difficult to understand the above Code after understanding each method
This code may be in doubt. $ Parent. find (". formtips"). remove ();
This statement ensures that only one element is prompted. If this sentence is not found, the system will add the prompt element.

Related Article

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.