来自 knockout 教程:
为什么会这样?
<p>First name: <input data-bind="value: firstName" /></p>
虽然这不是?
<p>First name: <input data-bind="text: firstName" /></p>
View 模型
// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI
function AppViewModel() {
this.firstName = "Bert";
this.lastName = "Bertington";
}
最佳答案
因为在 html 中,'input type="text"' 中键入的文本存储在名为 value 的属性中。
值绑定(bind)影响元素的值属性,文本绑定(bind)改变元素的内部文本。
通过写作
<input data-bind="text: firstName" />
您正在尝试更改输入元素的内容。并且输入不允许内容。
<input value="where the value binding writes its data">
where the text binding writes its data
</input>
关于javascript - KnockoutJS,文本与值绑定(bind)。为什么文本绑定(bind)不绑定(bind)到输入字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19451855/