获取点击行的ID

时间:2016-04-27 11:14:51

标签: javascript jquery jqgrid

伙计我正在使用jqgrid ...我想在没有先选择整行的情况下点击图片来获取该行的ID ..这是我的代码

<script type="text/javascript"> 
    
$(document).ready(function() {
  $("#jqGrid").jqGrid({
    url: 'data.json',
    datatype: "json",
    styleUI: "Bootstrap",
    colModel: [{
        label: 'Order ID',
        name: 'OrderID',
        key: true,
        width: 75,
        hidden: true
    }, {
        label: 'From Date',
        name: 'FromDate',
        width: 150,
        editable: true,
        edittype: "text",
        id: "ui-datepicker-div",
        editoptions: {
            dataInit: function(element) {
                $(element).datepicker({
                    autoclose: true,
                    format: 'yyyy-mm-dd',
                    orientation: 'auto bottom'
                });
            },

        },
    }, {
        label: 'To Date',
        name: 'ToDate',
        width: 150,
        editable: true,
        edittype: "text",
        editoptions: {
            dataInit: function(element) {
                $(element).datepicker({
                    autoclose: true,
                    format: 'yyyy-mm-dd',
                    orientation: 'auto bottom'
                });
            },

        },
    }, {
        label: 'Customer ID',
        name: 'CustomerID',
        width: 150
    }, {
        label: 'Ship Name',
        name: 'ShipName',
        width: 200
    }, {
        label: 'Row Data',
        name: 'RowData',
        align: 'center',
        formatter: function() {
            return "<img src='resources/icon.jpg' onclick='OpenDialog()' alt='Data Row' />";
            width = 15;
        }
    }, ],
    loadonce: true,
    ......
});

});

...........

function OpenDialog() {
var result = "";
var grid = $("#jqGrid");
var rowKey = grid.getGridParam("selrow");
rowData = grid.getLocalRow(rowKey);
for (var item in rowData) {
    if (item == 'RowData') {
        break;
    }
    result += rowData[item] + ', ';
}
alert(result);

}

任何帮助请如何只在点击图像时获取行的ID?...非常感谢..提前感谢

1 个答案:

答案 0 :(得分:1)

使用这个html:

onclick='OpenDialog(this)'

并在OpenDialog中使用最接近的jquery方法:

function OpenDialog(element) {
   var id = $(element).closest('tr').attr('id');
   ...
}