代码之家  ›  专栏  ›  技术社区  ›  Stefan0309

击倒.js在MVC中-html表中的绑定不起作用

  •  0
  • Stefan0309  · 技术社区  · 3 年前

    为什么我不能为某个表的标签中的按钮做点击绑定?如果我把按钮移到桌子外面就行了?

    <td>
        <div>
            You've clicked <span data-bind="text: numberOfClicks"></span> times
            <button data-bind="click: incrementClickCounter">Click me</button>
        </div>
                                      
    </td>
    

    虚拟机:

    define(['viewmodels/shell', 'durandal/services/logger', 'plugins/dialog', 'viewmodels/shell', 'toastr', 'knockout', 'kovalidationconfig', 'plugins/router', 'typeahead.bundle'],
        function (shell, logger, dialog, shell, toastr, ko, kvc, router, typeahead) {
            var vm = {
                activate: activate,
                shell: shell,
                data: ko.observableArray([]),
                close: function () {
                    $(window).off('popstate', vm.goBack);
                    $(window).off('resize', adjustModalPosition);
                    dialog.close(vm, 'cancel');
                },
                goBack: function () {
                    $(window).off('popstate', vm.goBack);
                    $(window).off('resize', adjustModalPosition);
                    dialog.close(vm, 'back');
                },
                editPreregisteredChildren: function () {
                    router.navigate("#/function/" + this.id);
                },
                incrementClickCounter : function() {
                var previousCount = this.numberOfClicks();
                this.numberOfClicks(previousCount + 1);
            }
                currentPage: ko.observable(1),
                itemsPerPage: ko.observable(10),
                hasNextPage: ko.observable(false),
                previousPage: previousPage,
                nextPage: nextPage,
                searchCriteria: ko.observable(''),
                applySearch: applySearch,
                locations: ko.observableArray([]),
                locationId: ko.observable(),
                LocationName: ko.observable(),
                exportHref: ko.observable("/spa/ExportSchedulings"),
                bindingComplete: function (view) {
                    bindFindLocationEvent(view);
                }
            };
    ...
    )};
    
    0 回复  |  直到 3 年前
        1
  •  1
  •   Razi Syed    3 年前

    表的主体很可能在一个数组上迭代,每一行代表的是数组项,而不是根vm。您需要绑定到“单击:$parent.incrementClickCounter增加“或”单击:$root.incrementClickCounter命令".

        2
  •  1
  •   DrPepper    3 年前

    有没有可能你处在一个嵌套的情况?当我绑定到具有多个视图模型的视图时,有时会遇到这种情况。尝试将data bind='with:nameofViewModel'添加到表数据标记:例如:

    <td data-bind='with: nameOfTheViewModel'>
        <div>
            You've clicked <span data-bind="text: numberOfClicks"></span> times
            <button data-bind="click: incrementClickCounter">Click me</button>
        </div>
                                      
    </td>