【问题标题】:You cannot apply bindings multiple times to the same element, knockout js problem不能多次对同一个元素应用绑定,淘汰js问题
【发布时间】:2020-12-09 17:52:23
【问题描述】:

我需要有关此错误的帮助。我试图通过从下拉列表中选择选项来更改 div 元素的可见性,但我一直收到此错误。

这是我的代码

.js 文件:

var viewModel = {
            currentSelect : ko.observable("CostPools")
        };
        ko.applyBindings(viewModel);

html 文件:

    <div class="container">
        <div class="row">
            <div class="col-sm-9">
                <h4 class="section-title">Actuals vs Budgeted Amounts for Cost Pools and IT Towers</h4>
            </div>
            <div class="col-sm-3">
                <select data-bind="value: currentSelect">
                    <option value="costPools">Show Cost Pools</option>
                    <option value="itTowers">Show IT Towers</option>
                    <option value="showBoth">Show Both</option>
                </select>
            </div>
        </div>
    </div>
    <!-- ko if: currentSelect() === 'costPools' -->
    <div style="width: 98%; height: 300px;" id="cost-pools-chart"></div>
    <!-- /ko -->
    <!-- ko if: currentSelect() === 'itTowers' -->
    <div style="width: 98%; height: 300px;" id="it-towers-chart"></div>
    <!-- /ko -->
    <!-- ko if: currentSelect() === 'showBoth' -->
    <div style="width: 98%; height: 300px;" id="cost-pools-chart"></div>
    <hr />
    <div style="width: 98%; height: 300px;" id="it-towers-chart"></div>
    <!-- /ko -->
    <!-- /ko -->

我需要根据下拉列表显示这两个 div。例如,如果选择了 Cost Pools,则显示 Cost Pools div,如果选择 IT Towers 选项,则显示 IT Towers div,最后,如果选择 Show Both,则显示两个 div 和 hr/。 问题是我不确定这里可能是什么问题,而且我之前从未实施过下拉列表,你能帮忙吗?提前谢谢你。

错误:未捕获的错误:您不能将绑定多次应用于同一个元素。

【问题讨论】:

    标签: knockout.js knockout-3.0


    【解决方案1】:

    问题在于我的代码如何绑定到 viewModel 并声明变量。

    【讨论】:

      猜你喜欢
      • 2016-04-29
      • 1970-01-01
      • 2013-10-16
      • 2014-10-21
      • 2015-01-06
      • 2017-01-20
      • 2020-03-12
      • 2017-05-05
      • 1970-01-01
      相关资源
      最近更新 更多