Use jquery to determine the number of words that can be entered in the input box, And jquery to determine the input box

Source: Internet
Author: User

Use jquery to determine the number of words that can be entered in the input box, And jquery to determine the input box

Effect

All code, paste available

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
<Style>
# Comment textarea {height: 100px ;}
# Comment p span {color: red ;}
</Style>
<Script src = "js/jquery-1.7.1.min.js"> </script>
</Head>

<Body>
<Div id = "comment">
<Textarea> </textarea>
<P> You can also enter <span> 300 </span> characters </p>
</Div>
 
<Script>
$ (Function (){
$ ('# Comment textarea'). keyup (function () {// triggered when the keyboard is up after the input character
Var txtLeng = $ ('# comment textarea'). val (). length; // assign the length of the input character to txtLeng
// Take the input value for determination
If (txtLeng> 300 ){
// When the input length is greater than 300, the span is 0.
$ ('# Comment p span'). text ('0 ');
// Extract the first 300 characters of the input content and assign it to fontsize
Var fontsize = $ ('# comment textarea'). val (). substring (0,300 );
// Display to textarea
$ ('# Comment textarea'). val (fontsize );
} Else {
// If the input length is less than 300, the span value is 300 minus the length.
$ ('# Comment p span'). text (300-txtLeng );
}
});
});
</Script>
</Body>
</Html>

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.