javascript 淘汰赛 JS valueUpdate 按键不适用于启用绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27071903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:54:35  来源:igfitidea点击:

Knockout JS valueUpdate keypress not working for enable binding

javascriptjqueryhtmlknockout.js

提问by andy_coder

I have a 'Save' button on my form that has knockout enable binding. I added valueUpdate: 'keypress' to my knockout binding expression hoping for save button to become enabled on keypress. I have a simple input textbox with value knockout binding. Instead, page still requiring user to click away for save button to become enabled. HTML below. when user types in input, canSave becomes true enabling save button.

我的表单上有一个“保存”按钮,它具有淘汰启用绑定。我将 valueUpdate: 'keypress' 添加到我的淘汰赛绑定表达式中,希望在按键时启用保存按钮。我有一个带有值淘汰绑定的简单输入文本框。相反,页面仍然需要用户单击离开以启用保存按钮。下面的 HTML。当用户输入输入时, canSave 变为 true 启用保存按钮。

<button data-bind="click: save, enable: canSave, valueUpdate: 'keypress'">Save</button>

<h3><strong>First Name: <input data-bind="value: firstName"></strong></h3>

回答by Retsam

The valueUpdatebinding only makes sense coupled with a valuebinding; it should go on your input, not on your button.

valueUpdate结合才有意义加上value结合; 它应该在您的输入上,而不是在您的按钮上。

<button data-bind="click: save, enable: canSave">Save</button>
<h3><strong>First Name: <input data-bind="value: firstName, valueUpdate: 'keypress'"></strong></h3>

Also, consider valueUpdate: 'input'if you're using browsers new enough to support it, or even the textInputbinding, if you're using KO >3.2

另外,valueUpdate: 'input'如果您使用的是 KO >3.2 ,请考虑您是否使用足够新的浏览器来支持它,甚至是textInputbinding

回答by thecodeparadox

Try this:

试试这个:

<button data-bind="click: save, enable: firstName().length > 0">Save</button>

<h3><strong>First Name: <input data-bind="value: firstName, valueUpdate:'keypress'"></strong></h3>

from you're code it seems, firstNameis an observable, if so then above code will do what you need. It will disable the button if there is not input else make it enable.

从你的代码看来,firstName是一个可观察的,如果是这样,那么上面的代码会做你需要的。如果没有输入,它将禁用该按钮,否则将其启用。