简体   繁体   English

jQuery停止通过ASP.NET MVC中的Ajax在更新的页面上工作

[英]jQuery stop working on updated page via Ajax in ASP.NET MVC

I have a ASP.NET MVC page of Events of a specific day. 我有一个特定日期的事件的ASP.NET MVC页面。 I'm also using jQuery datepicker and when I select a day, I create a function that send this selected day via Ajax to a MVC ActionResult method and this method will treat this day, will do all the database stuff and will return me all the Events of this selected day. 我还使用了jQuery datepicker,当我选择一天时,我创建了一个函数,该函数通过Ajax将所选的这一天发送到MVC ActionResult方法,该方法将处理这一天,将处理所有数据库事务并将所有结果返回给我所选日期的活动。 So far, so good... 到现在为止还挺好...

After that, I was trying to use jQuery cluetip to show me some Events details via Ajax and this also worked... partially !!! 在那之后,我尝试使用jQuery cluetip通过Ajax向我展示一些事件详细信息,这也起作用了…… 部分

Why partially?? 为什么部分? When I load the page for the first time (and in all first times), the tips worked great and all the Ajax stuff! 当我第一次(也是所有第一次)加载页面时,这些技巧非常有用,并且所有Ajax东西都可以使用! And in all first time loadings, the page will show the Events of today and this is not via Ajax, it's just a ViewData["events"] sending information for the page. 并且在所有首次加载中,页面将显示今天的事件,而这不是通过Ajax进行的,它只是一个ViewData [“ events”]发送页面信息。 But, if I select a day, the page is updated via Ajax, but the tips don't work anymore!! 但是,如果我选择一天,页面将通过Ajax更新,但是提示不再起作用! They don't show anything... I hover the links and nothing happens...nothing nothing. 他们什么也没显示...我将链接悬停,没有任何反应...什么都没有。

I think this was a cluetip problem, but for my surprise it wasn't... 我认为这是一个提示问题,但令我惊讶的不是...

Just for testing purposes, I created this in jQuery: 仅出于测试目的,我在jQuery中创建了此代码:

$(".myLink").click(function(){ alert($(this).attr("id")); }

When the page loads for the first time, this code works, but if I select another day and the page is updated, this jQuery code doesn't work anymore!! 当页面首次加载时,此代码有效,但是如果我选择另一天并且页面已更新,则此jQuery代码不再起作用! The click function doens't work at all!! 点击功能根本不起作用!

So, finishing this doubt... does anyone know what could be happening? 因此,解决了这个疑问……有人知道会发生什么吗? Why my jQuery codes don't work in ASP.NET MVC updated page via Ajax?? 为什么我的jQuery代码在通过Ajax的ASP.NET MVC更新页面中不起作用?

Thanks all!! 谢谢大家!

Use the live function when binding events- 绑定事件时使用live功能-

$(".myLink").live("click", function(){
    alert($(this).attr("id"));
});

This will ensure all elements with .myLink will be effected by the event, even if they are dynamically loaded later. 这将确保所有带有.myLink的元素都将受该事件影响,即使以后动态加载它们也是如此。

This doesnt work for every event though, so check the doc: http://docs.jquery.com/Events/live 但是,这不适用于每个事件,因此请检查文档: http : //docs.jquery.com/Events/live

Add your mouseover/click event handlers again on ajaxComplete. 再次在ajaxComplete上添加鼠标悬停/单击事件处理程序。 jQuery selectors don't always work on document elements that are added dynamically to the page, like those returned from an AJAX call. jQuery选择器并不总是对动态添加到页面中的文档元素起作用,例如AJAX调用返回的元素。

This code re-binds your event right after content population: 这段代码会在内容填充后立即重新绑定您的事件:

$.get("/getMyContent/" + eventID, "{}", function(content) {
  $(".contentArea").html(content);
  $(".eventLink").click(function(){
    doMyPopupEtc();
  });
});

First I thought I had a similar problem. 首先,我认为我有一个类似的问题。 It turned out though that I was assigning the cluetip twice which stopped it from working. 事实证明,尽管我两次分配了提示提示,但提示提示仍无法正常工作。

It has to do with the path. 它与路径有关。 When you reference your scripts, be sure to use <% Url.Content("~/path/to/script") %> 当您引用脚本时,请确保使用<%Url.Content(“〜/ path / to / script”)%>

Improbable, but you could be wiping out the JS that is responsible for showing the cluetip when the MVC updates the container as a result of an AJAX call. 不太可能,但是当MVC由于AJAX调用而更新容器时,您可能会清除负责显示提示的JS。 That is, of course, if that JS is declared inline, inside the document. 那就是,当然,如果该JS是在文档中内联声明的。

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

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