Jquery registration on page load and subsequent ajax post

Work with jQuery, which should be registered when loading the first page and then sending Ajax. the control to which this applies is located inside the update panel. now what i do

Registering the same function in both document.ready and sys.application.add.load , so it works for a control located inside the update and control panels that are outside the update panel.

 $(document).ready(function () { CheckMaxlength(); //If Text area is placed inside update panel then apply restriction for texarea size. Sys.Application.add_load(function () { CheckMaxlength(); }); }); 

I want to know what is the exact way to work with the controls that are inside the update panel and the external update panel.

+5
source share
1 answer

I'm not sure what you want to do, but I suspect delegated events are probably your answer.

Delegated events have the advantage that they can handle events from descendant elements that will be added to the document later. By selecting an item that is guaranteed to be present during the attachment of a delegated event handler, you can use delegated events to avoid having to attach and remove event handlers frequently.

For example, you can add functionality to all text areas no matter when they are added to the page using

 $( "body" ).on( "click", "textarea", function() { alert( $( this ).val() ); }); 

The first advantage of your situation is that any text field added to the update panel at any time will receive delegated functions.

In addition, if you want to target behavior in the text box inside the update panel as well as outside the update panel, you can also do this.

 $("body").on('click', 'textarea', function () { alert( "outside" + $(this).val()); }); $(".update-panel").on('click', 'textarea', function () { alert("inside: " + $(this).val()); //stop the event from propagating up to the body event.stopPropagation(); }); 

Also, note that when executing this code, only body and upload-container tags should be ready. A text area is not required, so you do not need to run it inside Sys.Application.add_load .

0
source

Source: https://habr.com/ru/post/1232268/


All Articles