简体   繁体   English

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

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

I am doing some work in PHP. 我正在用PHP做一些工作。 I have two php page 我有两个php页面

index.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>

on click of submit button I need to get the value of selected radio button and pass it to the function. 在单击提交按钮时,我需要获取所选单选按钮的值并将其传递给函数。

Please help me 请帮我

call just a function in youe onclick inline... 在您的onclick内联函数中仅调用一个函数...

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

and get checked radio value in your function.. 并在您的函数中获取检查的单选值。

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

try this 尝试这个

JAVASCRIPT 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();
}

HTML 的HTML

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

Provide action and method attribute to form first 提供动作和方法属性以首先形成

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

And than only you can get the value like below: 不仅如此,您还可以获得如下所示的值:

$vote = $_POST['vote'];

I hope this is what you are looking for. 我希望这是您要寻找的。

try: 尝试:

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

EDIT: noticed it is a radio input... so this won't work. 编辑:注意到这是一个无线电输入...所以这行不通。 You need something like: 您需要类似:

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 '';
}

And then do: 然后执行:

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

通过jquery,您可以像下面这样获取所选电台的值:

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

Try changing those 2: 尝试更改那些2:

<form name="myForm"> 

onclick="getValue(document.myForm);"

then the function: 然后函数:

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();
}

Call the function without any value and then read out the radiobutton inbetween your function like this: 调用没有任何值的函数,然后像这样读出函数之间的单选按钮:

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM