admin管理员组文章数量:1325137
When I load the page and change the option in the select menu, in debugging consoles I get an error saying that descrip() is not a function
<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>
When I load the page and change the option in the select menu, in debugging consoles I get an error saying that descrip() is not a function
<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>
Share
Improve this question
edited Jun 2, 2011 at 23:04
Patricrawley
asked Jun 2, 2011 at 22:57
PatricrawleyPatricrawley
131 silver badge4 bronze badges
3
- 5 If you're doing this in IE the problem might be that you have a textarea with the id="descrip", try changing that id. – david Commented Jun 2, 2011 at 23:05
- The code in your JavaScript function makes no sense -- you're creating an XMLHttpRequest object, putting the object into a string, and trying to "write" to a TEXTAREA. What are you trying to acplish? – user149341 Commented Jun 2, 2011 at 23:06
- @david you beat me to it, but ur ment came while I was writing my answer. – Cyberherbalist Commented Jun 2, 2011 at 23:10
3 Answers
Reset to default 4There are four issues I see here: your XMLHttpRequest
, your method of writing text to a <textarea>
, your method of getting the currently selected value of a <select>
, and your function shares the same name as an ID (a problem only in IE).
AJAX doesn't quite work the way you have it above, as unfortunate as that is. Instead, you have to jump through some hoops in order to get a request running and get its responseText back. Here is some example code:
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);
That is a very light example, but it does demonstrate the generic concept. For more on AJAX in general, see the MDC'S excellent tutorial on beginning AJAX.
Next, there is no write
function for a <textarea>
in the DOM. In order to change what is in the textarea, you need to use its value
property:
your_textarea.value = "something here";
Or if you want to append new text to the textarea:
your_textarea.value += "something here";
This will insert text properly.
Thirdly, your method of ascertaining the current selected <option>
's value in a <select>
also does not work (unfortunately). In order to grab the value of the currently selected option, you have to use the selectedIndex
property of a <select>
as well as its options
property:
your_select.options[your_select.selectedIndex].value;
This will properly return the value of the currently selected option.
Finally, and this is only an issue in IE, your function shares the same name as an ID. In IE, any IDs get defined globally as DOM elements, so this is an issue. So, simply changing your function's name to something else should alleviate that issue.
All-in-all, here is the code I believe works (though untested):
<script type='text/javascript'>
function select_change() {
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) {
var file = "includes/ads/" + xhr.responseText + ".txt.";
document.ads.descrip.value += file;
}
};
// Three arguments: type of request, url, and should the request be async
xhr.open("GET",
document.ads.subject.options[document.ads.subject.selectedIndex].value,
true);
xhr.send(null);
}
</script>
<form name="ads" method="post" action="scripts/del_advert_script.php">
<label>Advertisements by subject:</label><select name="subject" id="sub" onchange="select_change()">
//PHP Block that works
</select>
<textarea id="descrip" name="description" cols="100" rows="3" readonly="readonly">
</textarea>
<br />
<input type="submit" value="Submit" />
</form>
This is a wild guess, as I am by no means a Javascript person, but I am wondering if giving your textarea the id=descrip (same name as your function) might be confusing the interpreter?
In Internet Explorer, element ids get defined as constants on window
. The fact that your function is named the same as your element's id creates a conflict, and the element is winning, so IE sees you trying to call a textarea instead of a function.
本文标签: Javascript function not being recognized as a functionStack Overflow
版权声明:本文标题:Javascript function not being recognized as a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742172576a2426988.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论