淘汰IE8兼容性问题

时间:2013-05-06 10:07:41

标签: internet-explorer knockout.js ie8-compatibility-mode

在使用IE8兼容模式的IE9上运行时,我遇到了很多问题。 这是问题所在:

我有一组动态生成的文本框,并且更改事件绑定到这些元素。当更改事件出现时,在IE8兼容模式下,我需要按Tab键两次以远离控件。这不会发生在Firefox中,也不会发生事件限制。

我不确定这是否与我的活动有关,所以我在这里发布代码:

  <tbody data-bind="foreach: DailyItemList">
                            <tr>
                                <td>
                                 <span  data-bind="text:Day"></span>

                                </td>
                                <td><input id="Text2" data-bind="value: Required"/></td>
                                <td><input id="Text3" data-bind="value: Setup, event:{change: ValidateSetup}"/></td>
                                <td><input id="Text4" data-bind="value: Close, event:{change: ValidateClose}"/></td>
  </tr>
  </tbody>

以下是具有更改功能的视图模型:

var DailyItem = function (data) {
this.Day = ko.observable(data.DayOfWeek);
this.Required = ko.observable(data.Required);
this.Setup = ko.observable(data.SetupTime);
this.Close = ko.observable(data.CloseTime);
this.ValidateSetup = function () {
    if (this.Setup() > 30) {
        alert('Invalid SetupTime');
    }
}
this.ValidateClose = function () {
    if (this.Close() > 30) {
         alert('Invalid SetupTime');
    }
}
}

function ViewModel(items) {
   this.DailyItemList = ko.observableArray([]);
   records = $.map(items, function (data) { return new DailyItem(data) });
   this.DailyItemList(records);
}

1 个答案:

答案 0 :(得分:2)

如果您想解决IE8兼容性问题,可以从true函数返回Validate

this.ValidateSetup = function () {
    if (this.Setup() > 30) {
        alert('Invalid SetupTime');
    }

    return true;
}

以下是一个示例:http://jsfiddle.net/rniemeyer/FfNqv/(jsFiddle在IE8模式下有一些问题,因此您可以运行http://jsfiddle.net/rniemeyer/FfNqv/show/

更好的方法可能是使用manual subscription。只要单个observable发生更改,这是执行代码的好选择。同样可以在computed中完成,这允许您依赖多个可观察量。

通过手动订阅,您可以:

<input id="Text3" data-bind="value: Setup"/>

在您的视图模型中,您可以这样:

this.Close = ko.observable(data.CloseTime);
this.Close.subscribe(function(newValue) {
    if (newValue > 30) {
        alert('Invalid SetupTime');
    }

    //set other observables, or do whatever you like. "this" will be the this object.

}, this);
相关问题