douzhuanqian8244 2014-10-15 01:14 采纳率: 100%
浏览 149

使用ajax获取数据并更新表单字段

I added this code in the head:

<script>
function cpf(pag) {
    request = createRequest();
    if(request == null) {
        alert("Unable to Create Request");
        return;
    }
    var nocache = new Date();
    var url = "https://pathto.com/cp/gettime.php?t="+pag;
    url = url + "&stopIEcache="+ nocache;
    request.open("GET",url,true);
    request.onreadystatechange = showtpage;
    request.send(null);
}

function showtpage() {
    if(request.readyState == 4) {
        if(request.status == 200) {
            detailDiv = document.getElementByName("newtdf")[0];
            detailDiv.innerHTML = request.responseText;
        }
    }
}

function createRequest() {
    try {
        request = new XMLHttpRequest();
    } catch (tryMS) {
        try {
            request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (otherMS) {
            try {
                request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (failed) {
                request = null;
            }
        }
    }
    return request;
}

</script>

Here is my HTML output by php:

<input type="text" value="2014/10/10 05:08" id="datetimepicker" name="newtd" onBlur="cpf(document.getElementByName('newtd')[0].value);" /><br />
Epoch Timestamp Built: <input type="text" name="newtdf" size="10" value="">

So I have the first field that is selectable by a datetimepicker, when they leave the field (onblur), it should run the cpf function I created on the script, and pass the value of the field to it.

But it is not working.

I ran it and with the debugger, it says this:

Uncaught TypeError: undefined is not a function

can you help me fix it please?

  • 写回答

1条回答 默认 最新

  • doujia6433 2014-10-15 02:41
    关注

    Try declaring the request variable before, like the code below:

    <script>
    var request;
    function cpf(pag) {
        request = createRequest();
        if(request == null) {
            alert("Unable to Create Request");
            return;
        }
        var nocache = new Date();
        var url = "https://pathto.com/cp/gettime.php?t="+pag;
        url = url + "&stopIEcache="+ nocache;
        request.open("GET",url,true);
        request.onreadystatechange = showtpage;
        request.send(null);
    }
    
    function showtpage() {
        if(request.readyState == 4) {
            if(request.status == 200) {
                detailDiv = document.getElementByName("newtdf")[0];
                detailDiv.innerHTML = request.responseText;
            }
        }
    }
    
    function createRequest() {
        try {
            request = new XMLHttpRequest();
        } catch (tryMS) {
            try {
                request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (otherMS) {
                try {
                    request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (failed) {
                    request = null;
                }
            }
        }
        return request;
    }
    
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答