2013-05-13 67 views
4

如果您在Chrome中运行此提琴,选择框中将正确填充选项A,B和C.但是,如果您使用Internet Explorer(版本8或9)运行它,这是行不通的。我怎样才能修复这个小提琴使它与Internet Explorer一起工作,但仍然使用虚拟元素?敲除虚拟元素不适用于Internet Explorer

http://jsfiddle.net/jeljeljel/2tUmP/

HTML

<script type="text/html" id="template"> 
    <select id="type" name="type"> 
     <option value="">-- Choose --</option> 
     <!-- ko foreach: types --> 
     <option data-bind="text: $data.desc, attr: { value: $data.id }"></option> 
     <!-- /ko --> 
    </select> 
</script> 
<div id="placeholder" data-bind="template: { name: 'template' }"></div> 

的Javascript

function Model(){ 
    var self = this; 
    self.types = ko.observable([]); 
} 
var model = new Model(); 
model.types().push({id: 0, desc:'A'}); 
model.types().push({id: 1, desc:'B'}); 
model.types().push({id: 2, desc:'C'}); 

ko.applyBindings(model); 

回答

5

这可能是Internet Explorer的一个限制。

而不是虚拟的元素,用options结合来填充<select>元素:

<select id="type" name="type" 
    data-bind="options: types, optionsText: 'desc', optionsValue: 'id', optionsCaption: '-- Choose --'"> 
</select> 

文档:http://knockoutjs.com/documentation/options-binding.html

+1

应该指出的是,限制只能在'