简体   繁体   中英

How can one trigger a function onchange of a div?

Here's a question which has proved very difficult to find an answer online for.

Let's say a user runs an ajax function onclick to fill the contents of a div.

But, what if the ajax output is php, and for that onclick, i want other divs on the page to change in a manner that is dependent on the contents of div1?

This means I need to wait for div1 to actually change so i can use the ajax output of the php calculations to adjust div2 accordingly.

After some testing i've found out that i cant add a call to a second ajax function at the end of the first because it seems to run before the div content from the first ajax call actually changes.

So how can i trigger an ajax call onchange of the contents of a div?

所有ajax调用都会在调用完成后执行一个回调以运行,并将您的逻辑放在那里。

You could use Jquery

$('#div1').change(function(){
    doAjax( $(this).html() );
});

Or in the callback of the ajax

$.ajax({
    url: 'http://yoururl.com',
    type: 'post',
    data: { key: value },
    success: function( data ){
        doAjax( data );
    }
});

If you are aware of jquery, this is what you should try:

        $.ajax({
                traditional: true,
                type: "post",
                url: '<your_url>',
                beforeSend: function () {
                      //your logic to perform operation before ajax request
                },
                data: {
                    //data to send
                },
                success: function(response) {
                   //your logic to perform operation after ajax request
                   //here make another ajax request
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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