简体   繁体   中英

Document.ready() function

This is my ajax function

<script language="JavaScript" type="text/javascript">
 var num = 1;
function ajax_post(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "javas.php";



hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
    if(hr.readyState == 4 && hr.status == 200) {

        var return_data = hr.responseText;
        document.getElementById("status").innerHTML = return_data;
    }
}
// Send the data to PHP now... and wait for response to update the status div
hr.send("num=" + (++num)); // Actually execute the request
document.getElementById("status").innerHTML = "processing...";

}

Now i have this too find the correct div/class to run the ajax function in:

 $('.eventcontainer.button').click(function() { 
    $.post('javas.php', function(data) {
       $(this).parent('div').find('.status').html(data);
    })
    });

However im not sure where to implement this in my code

It's not a good idea to write your own ajax-request if you want to run your code on multiple browsers. If you have jQuery on your hand and you want a post ajax-request use the jQuery function:

$.post('ajax/test.html', function(data) {
    $('.result').html(data);
});

example for document ready to use:

function fooBar() {
//some code
}

$(document).ready(function(){
// all your jquery in here
$('body').hide().fadeIn(2000);
// or call your own functions
fooBar();
});

You can use this:

$(function(){
    $('.eventcontainer.button').click(function() { 
        $.post('javas.php', function(data) {
            $(this).parent('div').find('.status').html(data);
        })
    });
})

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