php - Document.ready() 函数

标签 php javascript jquery css ajax

这是我的ajax函数

<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...";

}

现在我也找到了正确的 div/类来运行 ajax 函数:

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

但是我不确定在我的代码中在哪里实现它

最佳答案

如果您想在多个浏览器上运行您的代码,那么编写您自己的 ajax 请求并不是一个好主意。如果您手头有 jQuery 并且想要发布 ajax 请求,请使用 jQuery 函数:

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

关于php - Document.ready() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8517947/

相关文章:

php - 单击按钮时删除动态创建的 dom 元素

javascript - 由于点 ID 导致 JSON 调用出错

javascript - $.ajax 与 $.post : getting allow origin error

javascript - 在图像标签之前插入 anchor 标签 jQuery

jQuery fadeIn 似乎没有运行?

javascript - 如何使用 CKEditor 自定义下拉插件添加样式

php - 如何配置 php_codesniffer 以强制 PHP 7.4 属性类型声明?

php - 有没有一种简单的方法可以读取目录中的文件名并将其添加到数组中?

php - 使用 file_get_contents() 加载远程 xml 页面

javascript - 有没有一种方法可以可变地命名函数