How to handle jQuery event on an ASP.net MVC Ajax loaded page?

女生的网名这么多〃 提交于 2019-12-24 02:04:54

问题


i've got an issue.

I'm a newbie in the world of jQuery Mobile, and with the ASP.Net MVC part I'm a little bit lost.

Here is my problem: In my mobile web site I want to change the navbar (that I use more like an appbar) buttons whereas I am on an Edit Page, or Home, etc....

So those pages (Edit, Show) are loaded in ajax, and on these pages I'm trying to fire an event like :

$(document).ready(function(){
   alert("Hello !");
});

But after some researchs, i found that in JQM with Ajax, events doesn't work that way, but more like this :

$(document).bind('pageinit', function(){
   alert("Hello !");
});

But this don't work for me (every page change the event is trigerred), maybe because in ASP.Net MVC Mobile we have only one data-role="page" and the rest of the content are loaded in ajax in the data-role="content", so I really don't know how I can fire an event when my "Ajax Loaded Kind of Page" is loaded ?

I had tried to live/bind on the listview of one of these pages but that does not work either :

$("[data-role='listview']").live('pageinit', function () {
    alert("hello");
});

To be more precise about what i'm trying to do : In ASP.Net MVC, my layout (who his common to all my pages) has a div with a data-role="page"attribute. This data-role is unique to my all app, and need to deal with it. But the fact is, when I load an other ASP.Net MVC Page in my code :

<div data-role="page" >
<div data-role="content"> Here my ASP.Net MVC Page</div>
</div>

I cannot use the $(document).bind('pagenit') because i don' load a page (data-role=page) but just a part of a data-role="content".

If you have any idea I will be glad to hear (more read) it, thanks in advance and sorry if my english is not really "understandable".


回答1:


Jquery Mobile gets the view through the AJAX and displays it. Due to this, the general events miss out since it is an AJAX call.

The best option is to disable Ajax for every form, you need to mention on each redirection call as well

data_ajax = "false"

Once it becomes a regular call, all Jquery events work normally!



来源:https://stackoverflow.com/questions/16034354/how-to-handle-jquery-event-on-an-asp-net-mvc-ajax-loaded-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!