-
Notifications
You must be signed in to change notification settings - Fork 0
/
测试extend-2.html
53 lines (45 loc) · 1.6 KB
/
测试extend-2.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
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p data-bind="css: { error: firstName.hasError }">
<input data-bind='value: firstName, valueUpdate: "afterkeydown"' />
<span data-bind='visible: firstName.hasError, text: firstName.validationMessage'> </span>
</p>
<p data-bind="css: { error: lastName.hasError }">
<input data-bind='value: lastName, valueUpdate: "afterkeydown"' />
<span data-bind='visible: lastName.hasError, text: lastName.validationMessage'> </span>
</p>
</body>
<script src="js/jquery.min.js"></script>
<script src="js/knockout.js"></script>
<script>
ko.extenders.required = function(target, overrideMessage) {
//console.log(overrideMessage);
//add some sub-observables to our observable
target.hasError = ko.observable();
target.validationMessage = ko.observable();
// 定义验证的方法
function validate(newValue) {
console.log(newValue)
target.hasError(newValue ? false : true);
target.validationMessage(newValue ? "" : overrideMessage || "必填项");
}
//初始化验证方法
validate(target());
// 只要值改变,就验证
target.subscribe(validate);
//return the original observable
return target;
};
// 声明
function AppViewModel(first, last) {
this.firstName = ko.observable(first).extend({ required: "Please enter a first name" });
this.lastName = ko.observable(last).extend({ required: "" });
}
ko.applyBindings(new AppViewModel("Bob","Smith"));
</script>
</html>