DOM如何获得Knockout JS值

[英]DOM how to get Knockout JS value


Having zero experience with Knockout but knowing Javascript and Jquery, I am facing a problem.

没有使用Knockout的经验,但知道Javascript和Jquery,我遇到了一个问题。

<input type="text" 
       value="Original Value" 
       data-bind="value: description, 
                  valueUpdate: 'afterkeydown'" />

var viewModel = { 
    description:ko.observable("")    
    };    

ko.applyBindings(viewModel);

http://jsfiddle.net/243b1z4h/1/

The user types something in that input text box, but the property value still has the old/original value (using the browser inspector tool) and does not update it.

用户在该输入文本框中键入内容,但属性值仍具有旧/原始值(使用浏览器检查器工具),并且不会更新它。

How am I able to force knockout to always update the input value?

我怎么能强制淘汰总是更新输入值?

enter image description here

1 个解决方案

#1


2  

This is only a "visual" question. The console doesn't realize that the element's value has changed, and keeps showing the original value attribute.

这只是一个“视觉”问题。控制台没有意识到元素的值已经改变,并且不断显示原始值属性。

To prove it, you can do the following:

为了证明这一点,您可以执行以下操作:

  • use Chrome's console pointer (on top left corner, or press Ctrl+Shift+C) and select the input element
  • 使用Chrome的控制台指针(在左上角,或按Ctrl + Shift + C)并选择输入元素

  • on the console type $0.value. It will show you the value as updated by ko
  • 在控制台上键入$ 0.value。它将显示由ko更新的值

In case you don't know what $0 is, it's the selected element. You can also use $0 for something interesting, like showing the ko bound viewmodel: ko.toJS(ko.dataFor($0))

如果您不知道$ 0是什么,那么它就是所选元素。您还可以使用$ 0表示有趣的内容,例如显示ko bound viewmodel:ko.toJS(ko.dataFor($ 0))

By the way, for this to work, you must choose the appropriate frame in your console: int the first textbox of the console, choose the result(fiddle.jshell.net/) frame

顺便说一句,要使其工作,您必须在控制台中选择适当的框架:在控制台的第一个文本框中,选择结果(fiddle.jshell.net/)框架


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/11/30/b094922e811f51127aa6041a4b9dceff.html



 
  © 2014-2022 ITdaan.com 联系我们: