dongshuobei1037 2014-08-24 17:07
浏览 47

函数动作后重新加载Javascript

I have webpage with many check boxes which call a Javascript function when clicked:

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

Javascript function:

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);
}

I would like the webpage to be refreshed each time a box is checked. With adding location.reload() at the end of the function, the page is reloaded but the update_db.php page is not called anymore. How can I reload the page after the record is updated?

  • 写回答

1条回答 默认 最新

  • doudao2407 2014-08-24 17:14
    关注

    You should do this through a callback function:

    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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀