Javascript 制作剔除应用绑定以将选择选项视为数字

Javascript 制作剔除应用绑定以将选择选项视为数字,javascript,html,knockout.js,Javascript,Html,Knockout.js,Im将敲除与html选择/选项结合使用(请参阅): 调用applyBindings时,self.Width和self.Height将从其初始值10键入“10”,从而导致重新计算函数 这似乎不是什么大问题,但在一个更复杂的解决方案中,我有一个PageSize属性(每页100/500/1000行),当这个属性被更改时,它会导致多个AJAX调用 有哪些(奇特的)解决方案可以克服这个问题?您可以尝试以下方法 self.Width = ko.observable(10); self.Width.subsc

Im将敲除与html选择/选项结合使用(请参阅):

调用
applyBindings
时,
self.Width
self.Height
将从其初始值10键入“10”,从而导致重新计算函数

这似乎不是什么大问题,但在一个更复杂的解决方案中,我有一个PageSize属性(每页100/500/1000行),当这个属性被更改时,它会导致多个AJAX调用


有哪些(奇特的)解决方案可以克服这个问题?

您可以尝试以下方法

self.Width = ko.observable(10);
self.Width.subscribe(function(newValue){
   if(typeof newValue === "string"){
       self.Width(parseInt(newValue));
   }
});

您可以将宽度设置为计算值,并写入自己的“写入”和“读取”选项,如下所示:

var _width = ko.observable(10);
self.Width = ko.computed({
  read : function(){
     return _width;
  },
  write: function(value){
     if(typeof value === "string"){
        _width(parseInt(value));
     }
  }

结帐是的,这就是我错过的。谢谢。我会坚持你的建议(类似于)。谢谢
self.Width = ko.observable(10);
self.Width.subscribe(function(newValue){
   if(typeof newValue === "string"){
       self.Width(parseInt(newValue));
   }
});
var _width = ko.observable(10);
self.Width = ko.computed({
  read : function(){
     return _width;
  },
  write: function(value){
     if(typeof value === "string"){
        _width(parseInt(value));
     }
  }