0

大家好,我想在 mvc 3 razor 中使用 konckout.js 创建一个网格,我在网格中有 3 列,第一列应该是按钮,其余列应该从控制器获取数据。事情是我能够实现这一切,但我无法为网格中的按钮设置类或 id 属性。我希望类为“选择”,其中按钮的 id 应该是后续列中的值,以便我可以执行一些使用 jquery 的客户端脚本。我的cshtml页面是

    <table>
<thead>
    <tr>
        <th>Buttons</th>
        <th data-bind="text: column1"></th>
        <th data-bind="text: column2"></th>
    </tr>
</thead>
<tbody data-bind="foreach: Office">
    <tr>
    <td><input type="button" value="Select"/></td>
    <td data-bind="text: name"></td>
    <td data-bind="text: location"></td>
    </tr>
</tbody>

我的视图模型是这样的

    var OfficeGrp = function(){
var self =this;
self.name = ko.observable();
self.location = ko.observable(); 
}
var OfficeVM = function () {
    var self = this;
    self.Office = ko.observableArray([new OfficeGrp()]);
    self.column1= ko.observable("Name");
    self.column2= ko.observable("Location");
    self.RadioBtnActive = function () {
        var self = this;
        self.Office.removeAll();
        $.getJSON(urlContent + "Office/GetOfficeGrid", { param: "true" }, function (data) {
            self.Office(data);
        });
    };
    self.RadioBtnAll = function () {
        var self = this;
        self.Office.removeAll();
         $.getJSON(urlContent + "Office/GetOfficeGrid", { param: "true" }, function (data) {
            self.Office(data);
        });
    };

};

$(document).ready(function () {
    var officeVM = new OfficeVM();
    ko.applyBindings(officeVM );
     officeVM.RadioBtnActive();
     $("input:button[class='select']").click(function () {
        alert('HELLO');
    });

});

即使我像这样明确地将类分配给按钮元素

    <input type="button" value="Select" class="select"/>

我给出的 Jquery 点击事件在点击按钮时没有被击中

    $("input:button[class='select']").click(function () {
        alert('HELLO');
    });

我已经尝试使用来自 knockout.js

    <input type="button" value="Select" class="select" data-bind="attr: { id: name}"/>

还有我忘记在开头添加的单选按钮

    <input id="RadiobtnActive" type="radio" name="radioBtn" checked="checked" class="rdbtn" data-bind="click: RadioBtnActive"/>Active
   <input id="RadiobtnAll" type="radio" name="radioBtn" class="rdbtn" data-bind="click: RadioBtnAll"/> All

但我首先希望 Jquery Button click 事件能够正常工作,但我想将 id 的值发送到控制器。在这种情况下的帮助将不胜感激............

4

1 回答 1

0

嗨,请尝试查看本教程

http://knockoutjs.com/examples/gridEditor.html

于 2013-11-06T05:57:59.063 回答