JQuery events don't work with ASP.NET MVC Partial Views -
I am trying to get JQuery events to work with partial views in ASP.NET MVC. However, after you load partial view through Ajax, JQuery is not able to fire events for any element in partial view. I suspect that if you are using other frameworks or javascript libraries to load partial HTML code with Ajax, then this issue will also happen. For example, consider the following example:
Controller:
Public Class Test Controller: Controller {Public Performance Index () {Return View (); } Public Performance LoadPortalView () {Return ("Partial View"); }}
Index.aspx
& lt; Script type = "text / javascript" & gt; $ (Document) .ready (function () {$ ("# button"). Click (function () {warning ('button click');});}); & Lt; / Script & gt; & Lt; Div & gt; & Lt;% (Ajax.BeginForm ("LoadPartialView", New Ajax Option {UpdateTargetId = "PartialView"})) {%> Click & lt; Input type = "submit" value = "here" /> & Lt;%}% & gt; to load partial view & Lt; / Div & gt; & Lt; Br / & gt; & Lt; Br / & gt; & Lt;% HTML.Renderpartial ("partial view"); & Gt%;
partialView.ascx
& lt; Div id = "PartialView" & gt; Click & lt; Input type = "button" id = "button" value = "here" & gt; & Lt; / Input & gt; To display a javascript message & lt; / Div & gt;
When the page loads for the first time, you can click "Click here to display JavaScript message" and you will get a JavaScript alert message that says "button clicked" . However, once you click on "Click here to load partial views", clicking on a button does not have any effect in fetching JavaScript alert messages. It seems that the 'click' event is no longer being left.
Do anyone know why this problem is with JQuery and how it is cured? This problem also occurs with other JQuery plugins that use events.
I have found a solution:
Just try to create:
$ (document) .on ("click", "YOUR_SELECTOR", function (e) {/// some stuff ...}); Instead of
:
$ ("YOUR_SELECTOR"). ("Click", function (e) {/// some stuff .. ..});
Comments
Post a Comment