【问题标题】:Submit radio buttons selection提交单选按钮选择
【发布时间】: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


【解决方案1】:

onsubmit 处理程序触发提交:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 2013-05-22
    • 2016-05-31
    相关资源
    最近更新 更多