函数动作后重新加载Javascript

时间:2014-08-24 17:07:39

标签: javascript php html reload

我的网页上有许多复选框,点击后调用Javascript函数:

<input type='checkbox' name='Box[]' value='3313' id='chk_3313' onclick='UpdateRecord(3313, this.checked);' >

Javascript功能:

function UpdateRecord(uid, chk) {
chk = (chk==true ? "1" : "0");
var url = "update_db.php?qso_id="+uid;
if(window.XMLHttpRequest) {
   req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
   req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("GET", url, true);
req.send(null);
}

我希望每次选中一个复选框时刷新网页。 通过在函数末尾添加location.reload(),可以重新加载页面,但不再调用update_db.php页面。 如何在更新记录后重新加载页面

1 个答案:

答案 0 :(得分:0)

您应该通过回调函数执行此操作:

function UpdateRecord(uid, chk) {
    chk = (chk==true ? "1" : "0");
    var url = "update_db.php?qso_id="+uid;

    if(window.XMLHttpRequest) {
       req = new XMLHttpRequest();
    } else if(window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
    }

    // Add an event listener to this request
    req.onreadystatechange = function(){
        // Check if the request is ready and didn't return any errors
        if (req.readyState == 4 && req.status == 200) {
            // Reload the page
            document.location.reload();
        }
    }

    req.open("GET", url, true);
    req.send(null);
}