【问题标题】:Knockout Jquery binding click does not work淘汰赛Jquery绑定点击不起作用
【发布时间】:2013-08-16 17:31:22
【问题描述】:

第一个问题!希望我能做好。

我有这个绑定列表:

<table id="restaurants_list" data-bind="foreach : restaurants" style="display: none">
    <tr>
        <td data-bind="text:name"></td>
        <td data-bind="text:address.address1"></td>
        <td data-bind="text:address.address2"></td>
        <td data-bind="text:address.postcode + ' ' + address.suburb"></td>
        <td>
            <input type="button" value="show" data-bind="click: $root.showmap" />
        </td>
    </tr>
</table>
<div id="map"></div>

这里是模型视图:

function RestaurantsViewModel() {
    var self = this;
    self.restaurants = data;
    self.showMap = function (restaurant) {
        $("#map").show();
        ....
    };
    showMap(restaurants[0]);
};

终于绑定了:

$(document).ready(function () {
    $("#link_get_restaurants").bind("click", get_restaurants);
});
function get_restaurants(event) {
    $("#restaurants_list").show();
    ko.applyBindings(new RestaurantsViewModel());
}

第一个showmap(restaurants[0]) 工作正常。但是,click : $root.showmap 不会触发。

那我做错了吗?我也用jquery,不知道能不能出这个。

谢谢。

【问题讨论】:

  • 当前示例中有一个类型:在您的视图模型中,您有 showMap,大写为 M,但在绑定中,您有 click: $root.showmap,小写为 m .将其更改为click: $root.showMap

标签: jquery binding knockout.js click


【解决方案1】:

试试这两个步骤:

1) 替换

click : $root.showmap

click : $root.showMap

2) 诅咒区分大小写:)

【讨论】:

  • 真丢脸!!!!!!非常感谢,花了 1 小时寻找 Jquery 和 Knockout 之间的复杂组合!
  • 第二步很重要!
【解决方案2】:

showmap 在绑定中拼写错误(您的方法是showMap)。你能尝试绑定到$root.showMap吗?

<input type="button" value="show" data-bind="click: $root.showMap" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-10
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 2013-03-01
    • 2013-12-27
    相关资源
    最近更新 更多