javascript - 获取点击时行的 id

标签 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 ??..非常感谢..提前致谢

最佳答案

使用这个html:

onclick='OpenDialog(this)'

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

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

关于javascript - 获取点击时行的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36888161/

相关文章:

jquery-plugins - jqgrid setGridParam 数据类型 :local

php - 将 Jquery 值传递给 PHP 脚本

javascript - Django 本地化内置小部件

javascript - 试图在印度 map 上应用等值线

jquery - CSS/JQuery : Positioning and resizing text with background image

javascript - 如何设置这个 "slider"每5秒改变一次

javascript - Phonegap - 输入类型电子邮件最大长度不起作用

javascript - mathjax 公式的下拉菜单

javascript - 初始滚动后脚本停止执行

jquery - 单击行时 JQGrid MultiSelect 复选框不会取消选中