<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication5.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="Scripts/jquery-2.0.3.min.js"></script> <script src="Scripts/knockout-2.3.0.js"></script> <script> var viewModel = function (a,b) { this.A = ko.observable(a); this.B = ko.observable(b); this.C = ko.computed(function () { return (this.A() / this.B()) * 100 + "%"; }, this); } $(function () { ko.applyBindings(new viewModel(1,2)); }) </script> </head> <body> <form id="form1" runat="server"> <input type="text" data-bind="value: A" /> <input type="text" data-bind="value: B" /> <span data-bind="text: C"></span> </form> </body> </html>
可以结合jquery和knockoutjs,实现数据模型与界面的双向绑定,这样就可以很灵活地实现
前两个文本框注册change事件即可~