سؤال

I have two input tags and I want the exact same value to be in both when I type in one. I am experimenting with knockout and am wondering how I would accomplish this.

<input type="text" data-bind="value: theValue" />
<br />
<input type="text" data-bind="value: theValue" />

<script type="text/javascript">
    var viewModel = {
    theValue: ko.observable("defaultText"),
};

ko.applyBindings(new ViewModel());
</script>
هل كانت مفيدة؟

المحلول

What you have is correct, you just want to add valueUpdate: 'afterkeydown' to your bindings so they update as you type.

<input type="text" data-bind="value: theValue, valueUpdate: 'afterkeydown'" />
<br />
<input type="text" data-bind="value: theValue, valueUpdate: 'afterkeydown'" />

Also, ko.applyBindings(new ViewModel()); should be ko.applyBindings(viewModel);

Here's a full demo

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top