【问题标题】:knockoutjs container less template inside SELECT, foreach OPTION not working with Internet ExplorerSELECT 中的 knockoutjs 容器少模板,foreach 选项不适用于 Internet Explorer
【发布时间】:2013-08-22 14:44:04
【问题描述】:

我有一个测验/调查应用程序,但在使用 knockoutjs 填充下拉列表时遇到问题。

请使用 Firefox(或 Chrome)检查此小提琴,然后使用 Internet Explorer 9(或 IE8 或 IE7...淘汰赛说与 IE6+ 兼容,但显然 IE 在我的情况下会出现一些问题)。

在 Firefox 和 Chrome my jsfiddle example 中有效,但不适用于 Internet Explorer。参见第 1 题,最后一个问题“你住在哪里?”在标签 2 中,“最喜欢的运动?”。 IE 中未填充下拉列表。

我怀疑 $parent 不能正常工作,或者 IE 不理解 $parent.ParticipantAnswer == null。我尝试调试但显然没有找到原因,所以我在这里。

在 tab1 和 tab2 中,选项都没有填充,所以这里是调用这两个模板的代码:

<!-- ko if: AnswerTypeId == 2 -->
<select data-bind="attr: { name: Id, id: 'answerq' + Id }">
<option value="0"></option>
<!-- ko template: { name: 'option-template', foreach: Answers } -->                                                        
<!-- /ko -->
</select>
<!-- /ko -->

<!-- ko if: AnswerTypeId == 6 -->
<select data-bind="attr: { name: Id, id: 'answerq' + Id }">
    <option value="0"></option>
    <!-- ko template: { name: 'location-template', foreach: Answers } -->                                                        
    <!-- /ko -->
</select>
<!-- /ko -->

这里有 2 个模板:

<script type="text/html" id="option-template">

    <!-- ko if: $parent.ParticipantAnswer != null && $parent.ParticipantAnswer.AnswerId == $data.Id -->
    <option data-bind="text: Description, attr: { value: Id, selected: 'selected' }"></option>
    <!-- /ko -->

    <!-- ko if: ($parent.ParticipantAnswer == null || $parent.ParticipantAnswer.AnswerId != $data.Id) -->
    <option data-bind="text: Description, attr: { value: Id }"></option>
    <!-- /ko -->

</script>

<script type="text/html" id="location-template">

    <!-- ko if: $parent.ParticipantAnswer != null && $parent.ParticipantAnswer.AnswerInt == $data.Id -->
    <option data-bind="text: Description, attr: { value: Id, selected: 'selected' }"></option>
    <!-- /ko -->

    <!-- ko if: ($parent.ParticipantAnswer == null || $parent.ParticipantAnswer.AnswerInt != $data.Id) -->
    <option data-bind="text: Description, attr: { value: Id }"></option>
    <!-- /ko -->              

</script>

我认为无容器模板会产生问题,但 this jsfiddle 可以在 Firefox 和 IE 上运行。

我真的不知道为什么它不适用于 IE,我在这里要求一个有效的修复程序,也许是一个原因的解释,所以我们都可以从中学习;)谢谢。

【问题讨论】:

  • 正如我在问题中指定的那样:IE9 和 IE8(以及 IE7)

标签: internet-explorer knockout.js template-engine


【解决方案1】:

一般来说,无容器绑定在 Internet Explorer 中运行良好。但是,IE(至少在 10 之前)会在某些情况下(例如 select 元素内部)剥离 cmets。

因此,KO 永远不会看到 cmets 或有机会处理它们。您最好的选择之一是使用repeat 绑定:https://github.com/mbest/knockout-repeat。这基本上可以让您通过附加到要重复的元素而不是容器来执行foreach

【讨论】:

  • 谢谢你,淘汰赛重复已经让我再次呼吸。在我的 ko if: AnswerTypeId == 6, 在选项 0 之后,我没有调用 location-template 我这样做: 见:jsfiddle.net/firepol/n93Bv/7。如何进行“如果”检查,例如 $parent.ParticipantAnswer.AnswerInt != $data.Id 以便为用户选择的选项添加属性 selected="selected"?感谢您让我知道... 更新 jsfiddle 代码会更好。
  • @firepol 将此绑定添加到您的选项中:attr: { selected: ParticipantAnswer.AnswerInt == $loc().Id }jsfiddle.net/n93Bv/8
  • @Micheal Best:成功了,谢谢!我还在 jsfiddle.net/n93Bv/9 更新了我的 jsfiddle。添加了额外的检查(如果 ParticipantAnswer 为空): attr: { selected: (ParticipantAnswer != null && ParticipantAnswer.AnswerInt == $loc().Id) } 。 RP Niemeyer:感谢大家向我推荐了 knout-repeat 并再次感谢 Micheal Best 开发了 knout-repeat(很酷的东西!这个解决方案让我免于 IE 的巨大痛苦)
猜你喜欢
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
  • 1970-01-01
  • 2013-07-25
  • 1970-01-01
  • 2014-01-04
相关资源
最近更新 更多