<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MyWeb.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="knockout-2.2.1.js" type="text/javascript"></script> </head> <body> <form id="form1" runat="server"> <div> <input id="Text1" type="text" data-bind="value: Text1" /> <input id="Text2" type="text" data-bind="value: Text2" /> <input id="Text3" type="text" data-bind="value: Text3" /> </div> </form> <script src="a/b/js1.js" type="text/javascript"></script> </body> </html>
var testKo = function () { self = this; self.Text1 = ko.observable("1"); self.Text2 = ko.observable("2"); self.Text3 = ko.dependentObservable(function () { if (parseInt(self.Text1()) > 10 && parseInt(self.Text2()) < -5) return parseInt(self.Text1()) + parseInt(self.Text2()); else return parseInt(self.Text1()) - parseInt(self.Text2()); }); } ko.applyBindings(new testKo());