错误消息Ajax,自我更新字段

时间:2017-10-15 09:55:27

标签: javascript php html ajax

我为自我更新字段制作了一个脚本。为什么我在此脚本中收到以下错误消息: UpdateField.html:37未捕获的ReferenceError:未定义fieldname     在HTMLInputElement.onchange(UpdateField.html:37)

脚本:

enter image description here

<!DOCTYPE html>

<html>
<head>
<script>

function update_person(str,fieldname,key,keyvalue) 
{
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = 
xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","update_person.php?
q="+str+"&f="+fieldname+"&n="+key+"&nv="+keyvalue,true);
        xmlhttp.send();
    }
}
</script>

</head>

<body>

<form>
<input type="text" name="firstname" data-fieldname ="PERSONFORNAVN" data-key 
="PERSONID" data-keyvalue = "7" 
onchange = "update_person(this.value,this.data-fieldname, this.data-
key,this.data-keyvalue)">
</form>    
<br>
<div id="txtHint"> </div>

    

1 个答案:

答案 0 :(得分:0)

  

你需要从第37行删除空格“?q =”就像“?q =”

function update_person(str,fieldname,key,keyvalue) 
{
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = 
this.responseText;
            }
        }
        xmlhttp.open("GET","update_person.php?q="+str+"&f="+fieldname+"&n="+key+"&nv="+keyvalue,true);
        xmlhttp.send();
    }
}
<html>
<head>



</head>

<body>

<form>
<input type="text" name="firstname" data-fieldname ="PERSONFORNAVN" data-key 
="PERSONID" data-keyvalue = "7" 
onchange = "update_person(this.value,this.data-fieldname, this.data-
key,this.data-keyvalue)">
</form>    
<br>
<div id="txtHint"> </div>