如何在 PHP 中使用 AJAX 获取选中的单选按钮值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2145990/
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-08-25 05:14:07  来源:igfitidea点击:

How to get checked radio button value using AJAX in PHP?

phpajaxradio-button

提问by SUN Jiangong

I'm going to get the checked radio button value using AJAX in PHP. I have done the following code, but i can't continue.

我将在 PHP 中使用 AJAX 获取选中的单选按钮值。我已经完成了以下代码,但我无法继续。

I need your help.

我需要你的帮助。

ajax_radio.php

ajax_radio.php

<html>
  <head>
    <script type="text/javascript" src="ajax.js" ></script>
  </head>
  <body>
    <input type="radio" name="radio1">Blue</input>
    <input type="radio" name="radio2">White</input>
    <input type="radio" name="radio3">Red</input>
    <input type="button" name="btn" value="Click" onClick="hello('a')"></input><br />
   <input type="text" id="btn_get" name="get_btn_value"></input>
  </body>
</html>

ajax.js

ajax.js

var xmlHttp;
function GetXmlHttpObject(){
    try{
        xmlHttp = new XMLHttpRequest();
    }
    catch(e){
        try{
            xmlHttp = new ActiveXObject(Msxml2.XMLHTTP);
        }
        catch(e){
            try{
                xmlHttp = new ActiveXObject(Microsoft.XMLHTTP);
            }
            catch(e){
                alert("doesn't support AJAX");
                return false;
            }
        }
    }
}
function hello(a){
    GetXmlHttpObject();
    xmlHttp.open("GET","ajax_radio.html?",true);
    xmlHttp.onreadystatechange = response;
    xmlHttp.send(null);
}
function response(){
    if(xmlHttp.readyState == 4){
        if(xmlHttp.status == 200){
            var radio_text = xmlHttp.responseText;
            document.getElementById('btn_get').innerHTML = radio_text;
        }
    }
}

Do you know how to complete this? Thanks in advance. :)

你知道如何完成这个吗?提前致谢。:)

Edit:

编辑:

I can't post the code here right now, because of the low speed of network. I'm sorry.

由于网络速度低,我现在无法在此处发布代码。抱歉。

I have shared it in rapidshare. Can you download it firstly, and help then? Thanks a lot.

我已经在Rapidshare 中分享了它。能不能先下载,然后帮忙?非常感谢。

I have to update it when i go back home. Too bad.

我回家后必须更新它。太糟糕了。

回答by RJD22

because I mostly use Jquery I wrote here a jquery snipped that checks radio button values because it is much easier and cleaner:

因为我主要使用 Jquery 我在这里写了一个检查单选按钮值的 jquery 剪辑,因为它更容易和更清晰:

<script src="http://www.google.com/jsapi"></script>
<script>
    google.load("jquery", "1.4.0");
</script>
<script type="text/javascript">
    $(function() {
        $('#buttoncheck').click(function () {
            var var_name = $("input[name='radio1']:checked").val();
            $('#btn_get').val(var_name);
        });
    });
</script>
<html>
    <head>
        <script type="text/javascript" src="ajax.js" ></script>
    </head>
    <body>
        <input type="radio" value="Blue" name="radio1">Blue</input>
        <input type="radio" value="White" name="radio1">White</input>
        <input type="radio" value="Red" name="radio1">Red</input>
        <input id="buttoncheck" type="button" name="btn" value="Click"></input>
        <br />
        <input type="text" id="btn_get" name="get_btn_value"></input>
    </body>
</html>

Take a look for jquery here: Jquery

在此处查看 jquery:Jquery

Explanation: This part selects the item with the ID #buttoncheck and checks if its ran: $('#buttoncheck').click(function () {

说明:这部分选择 ID 为 #buttoncheck 的项目并检查其是否运行: $('#buttoncheck').click(function () {

Then it will run the code in the function. This code gets the value of a input box with the name radio1. the ':checked' makes sure that it only selects the value of the checked radio button:

然后它将运行函数中的代码。此代码获取名为 radio1 的输入框的值。':checked' 确保它只选择选中单选按钮的值:

var var_name = $("input[name='radio1']:checked").val(); 

And last. This puts the value of the variable into the item with #btn_get as id:

最后。这会将变量的值放入以 #btn_get 作为 id 的项目中:

$('#btn_get').val(var_name);