提交单选按钮选择

时间:2014-06-04 20:57:37

标签: javascript ajax radio-button form-submit getelementbyid

我有这个代码用于单选按钮调查:

<div id="MyPoll">
How was the game?<br />
<form id="FirstPoll">
<input type="radio" name="Poll1" value="x" onclick="GetVote(this.value)" /> Awesome<br />
<input type="radio" name="Poll1" value="y" onclick="GetVote(this.value)" /> Awful<br />
<input type="submit" value="Submit">
</form>
</div>

“GetVote”功能是这样的:

function GetVote(int) {
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else  {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    document.getElementById("MyPoll").innerHTML=xmlhttp.responseText; }
  }
xmlhttp.open("GET","PollResults.php?Poll1="+int,true);
xmlhttp.send(); }

这样,当用户投票时,结果会直接显示,但我希望用户投票在显示之前传递给SUBMIT按钮。 我想我必须插入document.getElementById(“FirstPoll”)。submit();在我的“GetVote”函数中的某个地方(....在哪里?),从'input'中删除'onclick'事件并使用提交按钮触发“GetVote”函数? 任何建议都不仅仅是欣赏!

谢谢!

1 个答案:

答案 0 :(得分:0)

onsubmit处理程序触发提交:

<form id="FirstPoll" onsubmit="GetVote(this); return false;">
    ...
</form>

function GetVote(form) {
    var int = form.Poll1.value;
    // Code to submit via AJAX
}
相关问题