Javascript函数未被识别为函数

Javascript函数未被识别为函数,javascript,function,Javascript,Function,当我加载页面并更改“选择”菜单中的选项时,在调试控制台中,我会收到一个错误,指出descrip()不是函数 <script type="text/javascript"> function descrip(){ var aid = new XMLHttpRequest("GET",document.ads.subject.value,false); var file = "includes/ads/"+aid+".txt"; document.ads.d

当我加载页面并更改“选择”菜单中的选项时,在调试控制台中,我会收到一个错误,指出descrip()不是函数

<script type="text/javascript">
function descrip(){ 
    var aid = new XMLHttpRequest("GET",document.ads.subject.value,false);  
    var file = "includes/ads/"+aid+".txt";
    document.ads.descrip.write(file);
    return aid;
}
</script>

<form name="ads" method="post" action="scripts/del_advert_script.php">
<label>Advertisements by subject:</label><select name="subject" id="sub" onChange="descrip()">
        //PHP Block that works
    </select>
    <textarea id="descrip" name="description" cols="100" rows="3" readonly="readonly">

    </textarea>
    <br />
    <input type="submit" value="Submit" />
</form>

函数descripp(){
var-aid=new-XMLHttpRequest(“GET”,document.ads.subject.value,false);
var file=“includes/ads/”+aid+“.txt”;
document.ads.descrip.write(文件);
返回援助;
}
按主题划分的广告:
//有效的PHP块


这是一个猜测,因为我绝不是一个Javascript人,但我想知道给textarea id=descripp(与函数同名)是否会让解释器感到困惑

这是一个猜测,因为我绝不是一个Javascript人,但我想知道给textarea id=descripp(与函数同名)是否会让解释器感到困惑

在Internet Explorer中,元素ID在
窗口中定义为常量。函数的名称与元素的id相同这一事实会产生冲突,并且元素将获胜,因此IE会看到您试图调用textarea而不是函数。

在Internet Explorer中,元素id在
窗口中定义为常量。函数的名称与元素的id相同这一事实产生了冲突,元素正在获胜,因此IE看到您试图调用textarea而不是函数。

我在这里看到了四个问题:您的
XMLHttpRequest
,您将文本写入
的方法,获取当前选定的
值的方法,以及与ID同名的函数(仅在IE中存在问题)

AJAX的工作方式与上面的不同,尽管这很不幸。相反,为了运行请求并返回其responseText,您必须跳过一些障碍。下面是一些示例代码:

var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xhr.onreadystatechange = function () {
    // When the server has returned a response, and it is good, we're ready
    if (xhr.readyState === 4 && xhr.status === 200) {
        // do something with xhr.responseText
    }
};
// Three arguments: type of request, url, and should the request be async
xhr.open("GET", "url here", true);
xhr.send(null);
这是一个非常简单的例子,但它确实展示了一般概念。有关AJAX的更多信息,请参阅AJAX开头部分

接下来,DOM中的
没有
write
函数。要更改文本区域中的内容,您需要使用其
属性:

your_textarea.value = "something here";
your_select.options[your_select.selectedIndex].value;
或者,如果要将新文本附加到文本区域:

your_textarea.value += "something here";
这将正确插入文本

第三,您在
中确定当前所选
值的方法也不起作用(不幸的是)。要获取当前所选选项的值,必须使用
selectedIndex
属性及其
options
属性:

your_textarea.value = "something here";
your_select.options[your_select.selectedIndex].value;
这将正确返回当前选定选项的值

最后,这只是IE中的一个问题,您的函数与ID共享相同的名称。在IE中,任何ID都被全局定义为DOM元素,因此这是一个问题。因此,简单地将函数名更改为其他名称应该可以缓解这个问题

总而言之,以下是我认为有效的代码(尽管未经测试):


函数选择_更改(){
var xhr=XMLHttpRequest?新的XMLHttpRequest():新的ActiveXObject(“Microsoft.XMLHTTP”);
xhr.onreadystatechange=函数(){
//当服务器返回响应,并且响应良好时,我们就准备好了
如果(xhr.readyState==4&&xhr.status==200){
var file=“includes/ads/”+xhr.responseText+“.txt。”;
document.ads.descripp.value+=文件;
}
};
//三个参数:请求类型、url和请求是否为异步
打开(“获取”,
document.ads.subject.options[document.ads.subject.selectedIndex].value,
正确的);
xhr.send(空);
}
按主题划分的广告:
//有效的PHP块


我在这里看到了四个问题:您的
XMLHttpRequest
,您将文本写入
的方法,您获取
当前选定值的方法,以及您的函数与ID共享相同的名称(仅在IE中是一个问题)

AJAX的工作方式与上面的不同,尽管这很不幸。相反,为了运行请求并返回其responseText,您必须跳过一些障碍。下面是一些示例代码:

var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xhr.onreadystatechange = function () {
    // When the server has returned a response, and it is good, we're ready
    if (xhr.readyState === 4 && xhr.status === 200) {
        // do something with xhr.responseText
    }
};
// Three arguments: type of request, url, and should the request be async
xhr.open("GET", "url here", true);
xhr.send(null);
这是一个非常简单的例子,但它确实展示了一般概念。有关AJAX的更多信息,请参阅AJAX开头部分

接下来,DOM中的
没有
write
函数。要更改文本区域中的内容,您需要使用其
属性:

your_textarea.value = "something here";
your_select.options[your_select.selectedIndex].value;
或者,如果要将新文本附加到文本区域:

your_textarea.value += "something here";
这将正确插入文本

第三,您在
中确定当前所选
值的方法也不起作用(不幸的是)。要获取当前所选选项的值,必须使用
selectedIndex
属性及其
options
属性:

your_textarea.value = "something here";
your_select.options[your_select.selectedIndex].value;
这将正确返回当前选定选项的值

最后,这只是IE中的一个问题,您的函数与ID共享相同的名称。在IE中,任何ID都被全局定义为DOM元素,因此这是一个问题。因此,简单地将函数名更改为其他名称应该可以缓解这个问题

总而言之,以下是我认为有效的代码(尽管未经测试):


函数选择_更改(){
var xhr=XMLHttpRequest?新的XMLHttpRequest():新的ActiveXObject(“Microsoft.XMLHTTP”);
xhr.onreadystatechange=函数(){
//当服务器返回响应,并且响应良好时,我们就准备好了
如果(xhr.readyState==4&&xhr.status==200){
var file=“includes/ads/”+xhr.responseText+“.txt。”;
document.ads.descripp.value+=文件;
}
};
//三个参数:请求类型、url和请求是否为异步
打开(“获取”,