发现实际页面中是否有jqGrid对象

Lor*_*nzo 2 jquery jquery-plugins jqgrid jquery-selectors

问题在标题中几乎完成:有没有办法,使用jquery,找到由jqGrid对象处理的元素?

更确切地说,我希望在网格上调用重新加载方法(如果页面上有一个方法).我使用以下代码

...
success: function (data) {
    //Check if there is a jqGrid on the page and if present, reloads its data
    var jqGrid = $('.ui-jqgrid');
    if ( jqGrid.length ) {
        //get the grid id. The actual id object is in the form of "gbox_your_grid_id"
        var gridid = "#" + jqGrid.attr('id').substring(5);
        //time to reload
        $(grid).trigger('reloadGrid');
    }
}
Run Code Online (Sandbox Code Playgroud)

但似乎永远不会调用reloadGrid方法.有什么建议吗?

Ole*_*leg 5

很难建议任何完美的测试.您可以尝试例如搜索"gbox":$('div.ui-jqgrid')- 它是一个包含jqGrid的所有组件的div.如果$('div.ui-jqgrid').length > 0那时至少在页面上存在网格.

您可以搜索tablebdiv中的元素:

$('div.ui-jqgrid-bdiv table').length > 1
Run Code Online (Sandbox Code Playgroud)

甚至是

if ($('div.ui-jqgrid > div.ui-jqgrid-view > div.ui-jqgrid-bdiv > div > table.ui-jqgrid-btable').length > 1) {
    // jqGrid exist
}
Run Code Online (Sandbox Code Playgroud)

(详情请见此处).此外,您可以测试此类表元素是否具有jqGrid方法:

if ($.isFunction($('div.ui-jqgrid > div.ui-jqgrid-view > div.ui-jqgrid-bdiv > div > table.ui-jqgrid-btable').jqGrid)) {
    // jqGrid exist
}
Run Code Online (Sandbox Code Playgroud)