Jquery mobile asp.net mvc4,jquery mobile:jquery/javascript函数,用于捕获浏览器上从一个视图到另一个视图的更改

Jquery mobile asp.net mvc4,jquery mobile:jquery/javascript函数,用于捕获浏览器上从一个视图到另一个视图的更改,jquery-mobile,asp.net-mvc-4,Jquery Mobile,Asp.net Mvc 4,我有3个不同的视图共享同一母版页_layout.vbhtml 在每个视图中,用户必须选择不同的列表项。我正在使用sessionStorage存储选定的值。然后,在每次视图更改时,我想将这些值发布回服务器。为此,我在每个视图的末尾添加以下内容: $(window).unload(function () { alert("Bye now!"); postjson(); }); 但是,它并没有像预期的那样发挥作用。用户可以单击不同的按钮离开页面

我有3个不同的视图共享同一母版页_layout.vbhtml

在每个视图中,用户必须选择不同的列表项。我正在使用sessionStorage存储选定的值。然后,在每次视图更改时,我想将这些值发布回服务器。为此,我在每个视图的末尾添加以下内容:

      $(window).unload(function () {
        alert("Bye now!");
        postjson();
      });
但是,它并没有像预期的那样发挥作用。用户可以单击不同的按钮离开页面,但单击按钮时,窗口是否应该卸载并触发警报消息?不,它没有那样做

感谢您的帮助。

尝试使用

window.onbeforeunload = function() {
alert("Bye now!");
        postjson();
      });
可能来不及回答了

如果视图共享同一布局,当用户从一个视图移动到另一个视图时,他/她不会离开该视图。您应该使用jquery mobile

使用pagebeforehide或pagehide就可以了。这里是这些文件的摘录

前页隐藏

在fromPage上触发,我们正在转换 从开始,在实际过渡动画开始之前。回调 对于此事件,将接收一个数据对象作为其第二个参数。此数据 对象上具有以下属性:

    nextPage (object)
        A jQuery collection object that contains the page DOM element that we are transitioning to.

Note that this event will not be dispatched during the transition of the first page >     at application startup since there is no previously active page.
    nextPage (object)
        A jQuery collection object that contains the page DOM element that we just transitioned to.

Note that this event will not be dispatched during the transition of the first page at application startup since there is no previously
页面隐藏

转换动画完成后在fromPage上触发 完整的。此事件的回调将接收数据对象作为 他们的第二个arg。此数据对象具有以下属性:

    nextPage (object)
        A jQuery collection object that contains the page DOM element that we are transitioning to.

Note that this event will not be dispatched during the transition of the first page >     at application startup since there is no previously active page.
    nextPage (object)
        A jQuery collection object that contains the page DOM element that we just transitioned to.

Note that this event will not be dispatched during the transition of the first page at application startup since there is no previously
活动页面

您可以通过第二个页面访问prevPage或nextPage属性 绑定回调函数的参数


已经试过了,抱歉,我在原来的帖子中忘了提到。试试这个$window。绑定'beforeunload',函数{还要检查页面上是否有错误,jquery文件是否正确加载,这也不起作用。它一定是mvc4框架的某个东西。它只在我关闭选项卡或窗口时才会触发。它与mvc4无关。它应该可以工作。您的函数应该在document.readyTaskAdaque中,阅读此。使用jquery mobi建议您使用pageinit,而不是document.ready。在$document或$'div'上调用它们无法正常工作。您知道如何调用它们吗?谢谢您的回复。