knockoutjs 按钮点击事件不会触发

knockoutjs button click event doesn't fire

我正在根据 ajax 返回的数据构建 HTML,当我查看源代码时它看起来不错,但由于某些原因点击事件没有触发。

这就是我创建标记的方式:

$.ajax({
                    type: "POST",
                    url: "/webservices/WebService.asmx/GetData",
                    contentType: "application/json; charset=utf-8",
                    data: "{'orderId': " + JSON.stringify(order.OrderId) + "}",
                    dataType: "json",
                    success: function (data) {
                        if (data) {
                            if (data.d.length > 1) {
                                $.each(data, function () {
                                    $.each(this, function (k, v) {
                                        var temp2 = "<input type='button' class='btn' data-bind='value: " + v.TeacherId + ", click: $root.downloadImage' />";
                                        $(".downloadButtons").append(temp2);
                                    });
                                });
                                $("#selectOrderPackagePopup").modal("show");
                            }
                        }
                    },
                    error: function (n) {
                        alert('Error');
                    }
                });

然后我可以在模态弹出窗口中看到按钮,这是生成的源:

点击事件是这个:

self.downloadImage = function () {
                if (order) {
                    var url = "DownloadImage.aspx?orderId=" + order.id;
                    window.location = url;
                } 
            };

我无法触发点击事件。

将按钮添加到 DOM 后,您需要调用 ko.applyBindings。将它放在 ajax 成功回调中,例如 $("#selectOrderPackagePopup").modal("show"); 行下方。