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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14214585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:01:26  来源:igfitidea点击:

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

phpjavascriptjquery

提问by manas _Singh

I am doing some work in PHP. I have two php page

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

index.php

索引.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

请帮我

回答by bipen

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

爪哇脚本

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

回答by mipe34

Via jquery you can get value of selected radio like this:

通过 jquery,您可以获得所选收音机的值,如下所示:

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

回答by mallix

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

回答by metamagikum

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

回答by Suresh Kamrushi

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.

我希望这就是你要找的。

回答by Naryl

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);
    ....