我是淘汰赛和javascript的新手。我想在输入框上创建输入值,该值打印在document.write
上,但kotext
值不会在testText
上更新并更改输入框上的值。我希望保持打印出document.write
和array push(textKo)
进程的值。
来源:
<head>
<script src="knockout-3.1.0.js"></script>
</head>
<body>
<div class="sidebar">
<div class="line">
<label>1st Object Name</label>
<input type="text" data-bind="value: kotext1" />
<input type="text" data-bind="value: kotext2" />
<input type="text" data-bind="value: kotext3" />
</div>
</div>
<script>
var AppViewModel = function(){
kotext1 = ko.observable("No.1!");
kotext2 = ko.observable("No.2!");
kotext3 = ko.observable("No.3!");
}
ko.applyBindings(AppViewModel);
testText = function(val) {
return "This is " + val;
}
textKo = [testText(kotext1()),testText(kotext2()),testText(kotext3())];
document.write(textKo);
</script>
答案 0 :(得分:0)
看起来你误解了数据绑定在Knockout中是如何工作的。不要使用document.write
(which is a bad practice),而是更新视图模型:
var viewModel = {
kotext1: ko.observable("No.1!"),
kotext2: ko.observable("No.2!"),
kotext3: ko.observable("No.3!")
}
ko.applyBindings(viewModel);
testText = function (val) {
return "This is " + val;
}
viewModel.kotext1(testText(viewModel.kotext1()));
viewModel.kotext2(testText(viewModel.kotext2()));
viewModel.kotext3(testText(viewModel.kotext3()));