首先,您应该选择符合您要求的最佳回调。通常它会是onSelectRow
,但在其他一些情况下,另一个回调如onCellSelect
, beforeSelectRow
or ondblClickRow
更好。
在回调中你得到rowid
(the id
or the <tr>
row) 作为第一个参数。您可以使用getCell
, getRowData
or getLocalRow
获取某个单元格的包含。例如
onSelectRow: function (id) {
// get data from the column 'userCode'
var userCode = $(this).jqGrid('getCell', 'userCode');
alert(userCode);
}
or
onSelectRow: function (id) {
var localRowData = $(this).jqGrid('getLocalRow');
alert(localRowData.userCode);
}
如果 jqGrid 有本地数据(您使用datatype: 'local'
或远程数据类型,如datatype: 'json'
结合loadonce: true
).
UPDATED:在评论中发布一些帖子并更新您的问题文本后,我看到您使用用于 ASP.NET WebForms 的 jqSuite http://www.trirand.net/download.aspx或其他一些商业产品基于 jqGrid 而不是免费的开源 JavaScript 库jqGrid https://stackoverflow.com/tags/jqgrid/info。我不使用jqSuite,也不知道如何在jqSuite中实现JavaScript回调。
我可以建议您使用新的 jqGrid 4.3.2 功能:类似 jQuery 的事件 http://www.trirand.com/blog/?page_id=393/feature-request/usage-of-jquery-events-in-jqgrid-together-with-callback-functions-used-currently/#p25559。你能做的就是这样的代码
var $grid = jQuery("#<%= ModifyAccountUserDetailsjqGrid.ClientID %>");
$grid.bind("jqGridSelectRow", function (id) {
var userCode = $(this).jqGrid('getCell', 'userCode');
alert(userCode);
});
or
var $grid = jQuery("#<%= ModifyAccountUserDetailsjqGrid.ClientID %>");
$grid.bind("jqGridSelectRow", function (id) {
var localRowData = $(this).jqGrid('getLocalRow');
alert(localRowData.userCode);
});
像“jqGridSelectRow”这样的事件的事件处理程序可以在创建网格之前或之后定义(但在创建网格之后)<table>
元素与id
等于<%= ModifyAccountUserDetailsjqGrid.ClientID %>
被创建)。此外,如果需要,您可以将多个事件处理程序定义为一个事件处理程序。如果您希望在项目中为所有网格实施一些通用操作,那么这是非常实用的。