2012-09-13 61 views
0

我找不到爲什麼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(); 
} 

感謝。

+1

那麼有是名爲「投票」的兩個輸入元素,因此您不能僅僅通過這種方式獲取價值。你必須找到被檢查的那個。 – Pointy

回答

2

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)"