【发布时间】:2014-06-05 05:24:01
【问题描述】:
我有这个单选按钮投票的代码:
<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(); }
这样,当用户投票时,结果直接显示,但我希望用户投票在显示之前传递给提交按钮。 我想我必须插入 document.getElementById("FirstPoll").submit();在我的“GetVote”函数的某个地方(....哪里?),摆脱“输入”中的“onclick”事件并使用提交按钮触发“GetVote”函数? 任何建议都非常感谢!
谢谢!
【问题讨论】:
-
应该使用 jQuery 来简化此操作。
标签: javascript ajax radio-button form-submit getelementbyid