Programmatically updating a webshim date

无人久伴 提交于 2019-12-12 04:49:37

问题


I'm using an html5 <input type="date"> with a knockout binding. I'm using webshim for browsers that don't support html5 dates.

The native html5 browsers are working perfectly, changing the date changes the model, changing the model programmatically changes the date shown in the date input.

Changing the date on a browser that does not support html5 (e.g. IE8) works correctly and updates the knockout model, but the reverse is not true. Changes to the model are not propagated to the webshim-generated date picker, only to the hidden input that webshim uses.

Is there a webshim provided method or event I can call or fire to tell it to look at the data and update the UI after a change? How could I write a knockout binding to call this?


回答1:


It turns out webshim requires that you use jQuery().val() to update the date, rather than using the DOM directly. I was able to write a knockout binding that did this by extending the normal value binding:

ko.bindingHandlers.date = $.extend({}, ko.bindingHandlers.value);

ko.bindingHandlers.date.update =  function(element, valueAccessor) {
    // Set value using jQuery val method as this is caught internally by webshim
    $(element).val(valueAccessor()());
};

Then I could use:

<input type="date" data-bind="'date': date">

as would be expected.



来源:https://stackoverflow.com/questions/27292189/programmatically-updating-a-webshim-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!