MVVM schema ~knockoutjs series of text box number character length dynamic statistics function

Source: Internet
Author: User

Back to Catalog

Why do you write this function, because before doing a front-end page effect, using JS write, feel very tired, really, for a text box length dynamic statistics, you want to write Blur,press,down what event, too troublesome, at this time, I think of Knockoutjs, this thing good, Why, because it is simple enough, strong enough , these two points for the programmer, is good!

First look at the effect of the page

When the word count reaches a certain value, such as 10 words, then the text box will not allow you to enter again, this uses the subscribe, and the length of the link with the text box is used computed (dependentobservable dependent on the monitor is also possible), and when to trigger the event using the The valueupdate property Afterkeydown property value indicates that the keyboard is triggered when it is pressed.

Let's take a look at the implementation of the original code

HTML code

  <input type="text" data-bind="value:count,valueupdate: ' Afterkeydown '  " />  <span data-bind="text:countlen"></span>/  Ten  <span style="color:red; " data-bind=" ></span> "validationmessage:count"

JS Code

        /*study on the properties of computed Valueupdate*/Self.count=ko.observable (). Extend ({maxLength: {params:Message: "Maximum length is 10"}, Required: {params:true, message:"Please enter characters ..."            }        }); Self.count.subscribe (function(o) {//implements a character that is bound to a specified length when it is greater than a certain length            if(O.length > 10) Self.count (O.substr (0, 10));        }); Self.countlen= Ko.computed (function () {            returnSelf.count ()? Self.count (). Trim (). length:0; });

Back to Catalog

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.