0

我一直在为以下问题苦苦挣扎。我一直在网上搜索,但没有找到我的问题的答案。我有一个表格,我添加了一个带有数据绑定的表格。当我调用 ko.applyBindings(viewModel) 时,我没有收到任何错误,但里面的 HTML 被删除了。

我想要完成的是以下内容:

  1. 我有一个没有淘汰赛的生成的 HTML
  2. 我正在根据已知的输入 id 将淘汰赛绑定动态注入到 html 中。
  3. 我希望能够在顶层应用绑定,这将是我的表单,而不是添加每个单独的绑定。

代码

HTML
<form data-bind="with: form1">
<select data-bind="options: propertyNames, value: selectedProperty"></select>
<input data-bind="value: currentValue" />
<input data-bind="value: currentValue002" />
<button data-bind="click: setValue">Set Value</button>
<button data-bind="click: setValue2.bind($data, 'two', 'Jon')">Set two to Jon</button></form>


ViewModel:
<div data-bind="text: ko.toJSON($root.properties)"></div>
var viewModel = {
propertyNames: ["one", "two", "three"],
form1: ko.observable(),
properties: {
   one: ko.observable("Bob"),
   two: ko.observable("Ted"),
   three: ko.observable("Ann")
},
setValue: function() {
   this.properties[this.selectedProperty()](this.currentValue());   
},
setValue2: function(propName, value) {
    this.properties[propName](value);
}
};
ko.applyBindings(viewModel);

你可以在这里看到它:http: //jsfiddle.net/cricri99/Hk6MB/2/

我还尝试将 data-bind="submit: form1" 添加到我的表单中,然后用带有 data-bind="with: test" 的 div 包围内部 HTML,但这也不起作用。它还删除了内部 HTLM。

4

1 回答 1

0

请原谅这里的简短...

我添加了form1: ko.observable({currentValue: currentValue}),它并显示了表格。

但是,如果要在根目录上使用变量,请使用:

<form data-bind="with: form1"><input data-bind="value: $root.currentValue" /></form>
于 2014-06-13T17:38:16.533 回答