函数未将值作为参数传递
Function not passing value as argument
我不明白为什么getMoreInfoResults()函数没有将单选按钮的值(当选择单选按钮时)传递给GET请求。有人知道为什么吗?
<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>
这是我的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();
}
谢谢。
document.question_form.vote
表达式将为您提供NodeList
对象,而不是Node
对象。显然,它的value
性质是undefined
。
一个可能的解决方法是创建一个函数来检索选中单选按钮的值:
function getCheckedValue(radioNodes) {
for (var i = 0, l = radioNodes.length; i < l; i++) {
if (radioNodes[i].checked) {
return radioNodes[i].value;
}
}
}
并使用它而不是直接查询值:
onkeyup="getMoreInfoResults(getCheckedValue(document.question_form.vote), this.value)"
相关文章:
- 函数未将值作为参数传递
- 如何在HTML元素上创建函数,而不是将元素作为参数传递
- 如何传递函数中的参数
- 将参数传递给函数
- 如何向onClick事件处理程序传递一个接受参数的函数,并且仍然将该函数绑定到组件's”;这个“;上下文
- 传递带有参数的函数
- javascript,将参数传递给函数内部的闭包中的回调
- 将字符串作为参数传递给函数onclick event jquery
- 将修改后的数组作为参数传递给函数
- 代码气味-将布尔控制参数传递给函数
- 将参数传递给函数,同时保留事件处理程序
- 如何使用按钮将参数传递给函数
- 作为参数传递时如何计算函数
- 将对象作为参数传递时的 Javascript 函数作用域
- 将回调作为参数传递给函数
- 在构造控制器、服务等时作为函数参数传递之前列出的角度 JS 变量
- 作为参数传递的函数的异步执行
- 将请求作为参数传递给函数
- 什么时候需要在javascript中的函数中将对象作为参数传递
- Javascript,对象,用参数传递函数作为参数- angular, jquery概念基本误区