简体   繁体   English

敲除输入绑定

[英]Knockout input-binding

I am trying to update an application which didn't use Knockout earlier. 我正在尝试更新一个较早不使用Knockout的应用程序。 Few input fields gets data from the server/database as we change the select option. 当我们更改选择选项时,很少有输入字段从服务器/数据库获取数据。 Now, I am trying to generate a report based on the filled input fields using Knockout. 现在,我正在尝试使用Knockout基于填充的输入字段生成报告。 But when these input fields get data from database,it is not being updated on the UI. 但是,当这些输入字段从数据库获取数据时,不会在UI上对其进行更新。 For example, 例如,

 <input type = "text" id = "ip1" data-bind = "value:ip1"></input> 

gets data from database. 从数据库获取数据。

In the report I am trying to update the value received from database. 在报告中,我尝试更新从数据库接收的值。

 <tr><td>Left Margin:</td><td><strong data-bind="text:ip1"></strong></td></tr> 

View Model: 查看模型:

 function ViewModel() { self = this; self.ip1 = ko.observable(); }; var vm = new ViewModel(); ko.applyBindings(vm); 

The function for retrieving the data from the database is written separately using JavaScript, Ajax, PHP, xmlhttp. 使用JavaScript,Ajax,PHP和xmlhttp分别编写用于从数据库检索数据的功能。

Now when I am changing the select option, the input fields are getting updated but the on the report, the data is not being updated. 现在,当我更改选择选项时,输入字段正在更新,但是报表上的数据并未更新。

so it sounds like knockout is unaware that your elements have changed. 因此听起来像淘汰赛未意识到您的元素已更改。 can you force a change via jquery after your inputs change from your unbound event. 输入更改为未绑定事件后,可以通过jquery强制进行更改吗?

jQuery('#myinput').change();

here is a fiddle I did where I have an unbound button click that changes the value of a bound input box. 这是我做的小提琴,我单击了一个未绑定的按钮,即可更改绑定的输入框的值。 https://jsfiddle.net/othkss9s/30/ https://jsfiddle.net/othkss9s/30/

here is the javascript 这是JavaScript

function model() {
  var self = this;
  this.test = ko.observable("test");
};

var myViewModel = new model();

$(document).ready(function() {
  ko.applyBindings(myViewModel);
  $('#mybutton').click(function(event) {
    $('#myinput').val('changed from jquery');
    jQuery('#myinput').change();
  });

});

here is the html 这是HTML

<p>
  <input data-bind="textInput: test" id="myinput"> the value is:
  <label data-bind="text: test"></label>
</p>

<p>
  <button id="mybutton">
    change the value using jquery
  </button>
</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM