加载时jQuery无法正常工作 [英] On load jquery not working

查看:81
本文介绍了加载时jQuery无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有jqgrid,它会在调整大小时触发一个函数,以便它根据窗口大小调整大小.这个函数很好,但是当jqgrid加载时,我需要相同的函数才能工作 所以我这样调用函数

Hi I have jqgrid which on resize trigger a function so that it resizes according to window size. This function fine but I need that same function to work when the jqgrid loads so I am calling the function like this

$(window).bind("load", function() {
     $('#' + grid_id).setGridWidth(width, true); //Back to original width
    $('#' + grid_id).setGridWidth($('#' + div_id).width(), true); //Resized to new width as per window
    });

但是它不起作用.

像这样调整窗口大小时会调用相同的函数

The same function is called when the window is resized like this

$(window).bind('resize', function () {
            $('#' + grid_id).setGridWidth(width, true); //Back to original width
            $('#' + grid_id).setGridWidth($('#' + div_id).width(), true); //Resized to new width as per window
        }).trigger('resize');

它工作正常.我在这里做什么错了.

And it works fine. What is wrong am I doing here.

推荐答案

创建网格后,您只能在 之后调用jqGrid的setGridWidth方法.

You can call setGridWidth method of jqGrid only after the grid is created.

我想您可能需要添加选项autowidth: true.

I suppose that you need to do is probably adding the option autowidth: true.

如果要实现自己的自定义大小调整,则可以在jqGridAfterLoadComplete事件内部进行.该代码可能与以下内容有关:

If you want to implement your own custom resizing then you can do it inside of jqGridAfterLoadComplete event. The code could be about the following:

var $grid = $("#grid"),
    resizeGrid = function () {
        var newWidth = $(this).closest(".ui-jqgrid").parent().width();
        $(this).jqGrid("setGridWidth", newWidth, true);
    };

$(window).on("resize", function() {
    resizeGrid.call($grid);
});

$grid.on("jqGridAfterLoadComplete", resizeGrid)
.jqGrid({
    // all jqGrid options
});

请参见 https://jsfiddle.net/OlegKi/ejpn9/153/

这篇关于加载时jQuery无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆