[英]Function not passing value as argument
I can't figure out why the getMoreInfoResults() function doesn't pass on the value of the radio button (when one is selected) to the GET request. 我无法弄清楚为什么getMoreInfoResults()函数没有将单选按钮的值(当选择一个时)传递给GET请求。 Anyone know why? 谁知道为什么?
<form name="question_form">
<input type="radio" name="vote" value="1" onclick="getVote(this.value)" />Yes<br />
<input type="radio" name="vote" value="2" onclick="getVote(this.value)" />No<br />
<textarea rows="3" name="moreInfo" onkeyup="getMoreInfoResults(document.question_form.vote.value, this.value)" /></textarea><br />
<input type="submit" value="Submit" />
<div id="otherAnswers"></div>
</form>
This is my javascript: 这是我的javascript:
function getMoreInfoResults(vote, input) {
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("otherAnswers").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","phpPoll/phpPoll_userDefined/functions/getMoreInfoResults.php?vote=" + vote + "&moreInfo=" + input,true);
xmlhttp.send();
}
Thanks. 谢谢。
document.question_form.vote
expression will give you a NodeList
object, not a Node
one. document.question_form.vote
表达会给你一个NodeList
对象,而不是一个Node
一个。 Obviously, its value
property is undefined
. 显然,它的value
属性是undefined
。
One possible workaround is to create a function that will retrieve value of the checked radio button: 一种可能的解决方法是创建一个函数,该函数将检索已检查单选按钮的值:
function getCheckedValue(radioNodes) {
for (var i = 0, l = radioNodes.length; i < l; i++) {
if (radioNodes[i].checked) {
return radioNodes[i].value;
}
}
}
... and use it instead of querying the value directly: ...并使用它而不是直接查询值:
onkeyup="getMoreInfoResults(getCheckedValue(document.question_form.vote), this.value)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.