Javascript code _ javascript skills

Source: Internet
Author: User
The javascript code is used to calculate the remaining length of the text field. This Code is also a simple implementation method.

Remaining words in the text field calculated by JS <textarea cols = "20" rows = "4" onkeyup = "displaySpareNumber (this, 10)" onchange = "displaySpareNumber (this, 10) "> </textarea> <script type =" text/javascript "language =" JavaScript "> function displaySpareNumber (_ this, size) {var spareNumber = document. getElementById ("spareNumber"); // Chinese Character length var len = _ this. value. replace (/[^ \ x00-\ xff]/gi, 'xx '). length; var snum = parseInt (size)-len; spareNumber. value = snum; If (snum <0) {if (_ this. value. length! = Len) {if (len-_ this. value. length)> (size/2) {_ this. value = _ this. value. substring (0, size/2);} else {_ this. value = _ this. value. substring (0, size-(len-_ this. value. length) ;}} else {_ this. value = _ this. value. substring (0, size);} spareNumber. value = 0; return ;}} <input id = "spareNumber" name = "spareNumber" value = "10" size = "1"/> <br> note: <br> displaySpareNumber (this, 10) <br> and <br> <input id = "spareNumber" name = "spareNumber" value = "10" size = "1"/> <br> You define 10 content Length.
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.