-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-3KO.html
43 lines (33 loc) · 938 Bytes
/
1-3KO.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<p>Enter a numeric value:<input data-bind="value: attemptedValue"/></p>
<div data-bind="visible: !lastInputWasValid()">That's not a number!</div>
</body>
<script type='text/javascript' src='js/knockout.js'></script>
<script>
var ViewModel = function() {
this.acceptNumberValue = ko.observable(123);
this.lastInputWasValid = ko.observable(true);
this.attemptedValue = ko.computed({
read:this.acceptNumberValue,
write:function(value) {
if(isNaN(value)) {
this.lastInputWasValid(false);
} else {
this.lastInputWasValid(true);
this.acceptNumberValue(value);
}
},
owner:this
});
};
//实例化一个viewModel
var currentViewModel = new ViewModel();
ko.applyBindings(currentViewModel);
</script>
</html>