【问题标题】:Radio buttons not passing values via AJAX单选按钮不通过 AJAX 传递值
【发布时间】:2012-12-19 08:47:26
【问题描述】:

有谁知道为什么选中的复选框的值没有被回显?

Javascript 代码:

<script src="jquery.min.js" type="text/javascript"></script>
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.form.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
<!--
// wait for the DOM to be loaded 
$(document).ready(function()
{
    // bind 'vgsForm' and provide a simple callback function 
    $('#vgsForm').ajaxForm(function()
    {
        $('#Suggestion').load('process_answers.php');
    }); 
});

HTML 表单

<div id="Questions">
<form id="vgsForm" action="process_answers.php" method="get" >
<div id="Q1">
<label><input type="radio" name="q1option" value="Less than 16" />Less than 16</label><br />
<label><input type="radio" name="q1option" value="16 or more" />16 or more</label>
</div>

process_answers.php

echo('$_GET: '.print_r($_GET, true));

//Get Question 1
if (isset($_GET['q1option'])) 
{
    $q1option = $_GET['q1option'];
} 
else 
{
    $q1option = NULL;
}

echo("Selected: ".$q1option);

这是回应:

$_GET: 数组 ( ) 已选择:

感谢任何帮助

丹尼尔

附:这是我得到 JavaScript 代码的地方http://malsup.com/jquery/form/

是否需要任何额外的 JavaScript 代码才能使其工作?

【问题讨论】:

    标签: jquery html ajax forms radio-button


    【解决方案1】:

    您的 HTML 和内联 JS 代码似乎都有问题。 为您的表单尝试以下代码:

    <div id="Questions">
    <form id="vgsForm" action="process_answers.php" method="POST" >
       <label><input type="radio" name="q1option" value="Less than 16" />Less than 16</label><br />
       <label><input type="radio" name="q1option" value="16 or more" />16 or more</label>
       <input type="submit" value="Submit" />
    </form>
    </div>
    

    HTML 可能看起来像您从链接中获得的那个。除了jquery,你还必须包含来自http://malsup.github.com/jquery.form.js的脚本,它实现了表单ajax升级。

    <html> 
    <head> 
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
        <script src="http://malsup.github.com/jquery.form.js"></script> 
    
        <script> 
            // wait for the DOM to be loaded 
            $(document).ready(function() { 
                // bind 'vgsForm' in order to upgrade form to use ajax 
                $('#vgsForm').ajaxForm(function() { 
                    alert("Form was sent successfully."); 
                }); 
            }); 
        </script> 
    </head> 
    

    至于process_answers.php,请确保您从 POST 全局数组中读取,因为我们已将其设置为表单中的方法:

    echo('$_POST: '.print_r($_POST, true));    
    //Get Question 1
    if (isset($_POST['q1option'])) 
    {
        $q1option = $_POST['q1option'];
    } 
    else 
    {
        $q1option = NULL;
    }
    
    echo("Selected: ".$q1option);
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-30
      • 2015-10-04
      • 1970-01-01
      相关资源
      最近更新 更多