简体   繁体   English

在ScriptManager.RegisterStartupScript之后的代码中调用脚本后,jQuery document.ready不会触发

[英]jQuery document.ready doesn't fire after script called in code behind ScriptManager.RegisterStartupScript

I have a filtered list of items using https://mixitup.kunkalabs.com/ 我有使用https://mixitup.kunkalabs.com/过滤的项目列表

see screen for example 例如看屏幕 格

The user then selects to add another action - this uses fancybox as a popup and shows the user the next image: 然后,用户选择添加另一个操作-这使用fancybox作为弹出窗口,并向用户显示下一张图像: 弹出

Once the user has added the data, the pages does a partial post back using an update panel. 用户添加数据后,页面将使用更新面板进行部分回发。 the code then calls a js function from the code behind to close the popup and then call the mixitup function to reinitialize the list as it now has a new item in it, but it doesn't work. 然后,该代码从后面的代码中调用js函数以关闭弹出窗口,然后调用mixitup函数重新初始化列表,因为该列表中现在有一个新项,但是不起作用。

Here's the code behind that calls the js 这是调用js的背后代码

ScriptManager.RegisterStartupScript(Me, [GetType](), "Load", "ReloadData();", True)

and here is the js that will be called: 这是将被调用的js:

function ReloadData() {
    parent.jQuery.fancybox.close();
    $(document).ready(function () {
        $('#Container').mixItUp({layout: {display: 'block'}});
    });
}

The fancybox closes but the mixitup never gets fired as it it never passes the docment.ready test meaning its not loaded so cant run the jQuery, but I am unsure why? fancybox关闭,但mixitup从未触发,因为它从未通过docment.ready测试,这意味着未加载它,因此无法运行jQuery,但是我不确定为什么吗?

$(document).ready() fires after the document is finished rendering. $(document).ready()在文档渲染完成后触发。 So, if you already rendered your page and are now interacting with it, that ready statement no longer has any relevance, until you reload the page. 因此,如果您已经渲染了页面并且现在正在与之交互,则在重新加载页面之前,该ready语句不再具有任何意义。 So, don't put that inside a function. 因此,请勿将其放在函数中。

Just call your mixItUp code inside the function block. 只需在功能块内调用您的mixItUp代码即可。

function ReloadData() {
    parent.jQuery.fancybox.close();
    $('#Container').mixItUp({layout: {display: 'block'}});
}

As a suggestion: if you're already using jQuery, don't bother with update panels. 作为建议:如果您已经在使用jQuery,请不要理会更新面板。 Just do your ajax calls through $.ajax . 只需通过$.ajax ajax调用即可。

You should place the following code outside the ReloadData() function. 您应该将以下代码放在ReloadData()函数之外。

$(document).ready(function () {
        $('#Container').mixItUp({layout: {display: 'block'}});
    });

document ready function should always be kept at the top level. 文件就绪功能应始终保持在最高级别。 Also please check if there is any JS error while loading page. 另外,请在加载页面时检查是否有JS错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM