获取单选按钮onclick的选定值并将值传递给函数

get selected value of radio button onclick of submit and pass value to a function

本文关键字:值传 函数 onclick 单选按钮 获取      更新时间:2023-09-26

我在PHP做一些工作。我有两个php页面

index . php

<html>
<head>
<script>
function getVote(int)
{
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("poll").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","poll-vote.php?vote="+int,true);
xmlhttp.send();
}
</script>
</head>
<body>
<h3>Is this correct? </h3>
<div id="poll">
<form>
Yes:
<input type="radio" name="vote" value="0">
<br>No:
<input type="radio" name="vote" value="1">
<input type="submit" value="Forward" onclick="getVote(value);"> 
</form>
</div>
</body>
</html>

在点击提交按钮时,我需要获得所选单选按钮的值并将其传递给函数。

请帮帮我

在你的onclick中调用一个函数。

 <input type="submit" value="Forward" onclick="getVote();"> //here

并在你的函数中得到检查的无线电值。

var radiovalue= $('input[name="vote"]:checked').val()

试试这个JAVASCRIPT

function getVote()
{
 var radiovalue= $('input[name="vote"]:checked').val()
 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("poll").innerHTML=xmlhttp.responseText;
  }
}
xmlhttp.open("GET","poll-vote.php?vote="+radiovalue,true);
xmlhttp.send();
}

<input type="submit" value="Forward" onclick="getVote();"> 

为form first提供动作和方法属性

<form action="index.php" method="post">

然后你可以得到如下的值:

$vote = $_POST['vote'];

我希望这是你正在寻找的

try:

function getVote()
{
    var int=document.getElementById("vote").value;
    ....

编辑:注意到这是一个无线电输入…所以这行不通。您需要这样的内容:

function getRadioCheckedValue(radio_name)
{
   var oRadio = document.forms[0].elements[radio_name];
   for(var i = 0; i < oRadio.length; i++)
   {
      if(oRadio[i].checked)
      {
         return oRadio[i].value;
      }
   }
   return '';
}

然后做:

function getVote()
{
    var int=getRadioCheckedValue(vote);
    ....

通过jquery,你可以像这样获得选中的单选值:

$('input[name=vote]:checked').val()

试着改变这2:

<form name="myForm"> 
onclick="getValue(document.myForm);"

则函数:

function getValue(form){
 var value = '';
 for (var i = 0; i < form.elements.length; i++ ) {
   if (form.elements[i].type == 'radio') {
         if(form.elements[i].checked == true) {
             value = form.elements[i].value;
        }
     }
   }
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("poll").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","poll-vote.php?vote="+value,true);
xmlhttp.send();
}

调用不带任何值的函数,然后读取函数之间的单选按钮,如下所示:

var rval = document.getElementsByName('vote');
for (var i = 0, length = rval.length; i < length; i++) {
    if (rval[i].checked) {
        alert(rval[i].value); // your value
    }
}