简体   繁体   English

Knockout.js按钮单击事件不会触发

[英]knockoutjs button click event doesn't fire

I'm building HTML from data which is returned from ajax, it's looking good when I view the source, but for some reason the click event doesn't fire. 我正在从ajax返回的数据构建HTML,当我查看源代码时看起来不错,但是由于某些原因,click事件不会触发。

This is how I'm creating the markup: 这就是我创建标记的方式:

$.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');
                    }
                });

Then I'm able to see the buttons in the modal popup, and this is the generated source: 然后,我可以看到模式弹出窗口中的按钮,这是生成的源:

在此处输入图片说明

The click event is this one: 点击事件是此事件:

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

I'm not able to fire the click event. 我无法触发点击事件。

You need to call ko.applyBindings after you have added the buttons to the DOM. 将按钮添加到DOM后,需要调用ko.applyBindings Place it in your ajax success callback, eg below the $("#selectOrderPackagePopup").modal("show"); 将其放在您的ajax成功回调中,例如,在$("#selectOrderPackagePopup").modal("show"); line. 线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM