Knockout.js - 传递参数

时间:2014-11-04 09:30:53

标签: javascript knockout.js

传递参数有问题,当我按下按钮时,没有任何事情发生且控制台中没有错误

<tbody data-bind="foreach: hotels">
<tr>
   <td data-bind="text: Id"></td>
   <td data-bind="text: Name"></td>
   <td data-bind="text: Address"></td>                      
   <td>
       <button data-bind="click: $root.eksportuj" > @Html.Text("Eksportuj")/button>
   </td>
</tr>

self.eksportuj = function (obj, event) {
    $.ajax({
        ulr: "@Url.Action("eksport", "Controller")",
        type: 'POST',
        data: {'Id' : obj.Id},
        success: function (result)
           {
               alert("True");
           },
        error: function()
           {
               alert("Error");
           }
    });
}

1 个答案:

答案 0 :(得分:0)

问题可能出在非封闭式button代码或tbody代码

<button data-bind="click: $root.eksportuj" > @Html.Text("Eksportuj") </button>

Simle JSFiddle DEMO按预期工作

JSFiddle DEMO代码:

var viewModel = new function()
{
    var self = this;
    self.hotels = ko.observableArray([{
        Id : "1",
        Name : "N1",
        Address : "A1"
    },{
        Id : "2",
        Name : "N2",
        Address : "A2"
    }]);

    self.eksportuj = function (obj, event) {
       alert(obj.Id)
    }
}

ko.applyBindings(viewModel)