Each function in jquery is a small note

Source: Internet
Author: User

In the project itself, when writing a very simple form validation, you need to traverse all textarea and judge. Because of jquery, the first reaction is to use each function traversal, when traversing to an empty textarea, pop-up hints and interrupt the operation of JS. At first I wrote this:

 1  Input.each (function   () { 2  if  ($ (this ). val () = = null  )  3   { 4  alert ("Test" 
     7  //  other code  8 }); 

After running, the popup will appear, but then the code is run, and all the textarea are traversed.

After looking at the source code of each () function (in fact, not thoroughly), the first thought is to use a for loop instead. The code is simply not written. Another method is to throw an exception. Examples are as follows:

1         Try2     {3Input.each (function(){4             if(!empty_validation ($ ( This)))5                 Throw false;6         });7     }    8     Catch(e)9     {Ten         return false; One}

Each function in jquery is a small note

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.