为什么我无法通过单选按钮获得价值?

时间:2018-01-09 19:34:53

标签: javascript php jquery ajax radio-button

我很难通过JQuery获得一组单选按钮的价值。我不确定问题是在.val()函数还是在ajax请求中。 我的单选按钮代码是:

<fieldset class="fieldset">
            <legend>New User Signup:</legend>
            <label class="left">Username: </label><input type="text" name="username" id="username" placeholder="username" size="16"> <br >

            <label class="left">Password: </label><input type="password" name="password" id="password" placeholder="password" size="16"> <br >

            <label class="left">Gender: </label><input type="radio" name="cc" value="M"> Male <input type="radio" name="cc" value="F" checked="checked"> Female <br >

            <label class="left" >Birth Date: </label><input type="text" id="datepicker" name="age" size="10" maxlength="10"> <br >

            <input type="submit" id="signup_button" value="Sign Up"> 
</fieldset>

而我的js代码是:

jQuery(document).ready(function($) {
$("#signup_button").click(function(){
    var username = $("#username").val();
    var pass = $("#password").val();
    var gender = $('input[name=CC]:checked').val();
    var birth = $("#datepicker").datepicker("getDate");

    $.ajax({
        method: "POST",
        url: "register.php",
        data: {
            username: username,
            pass: pass,
            gender: gender,
            birth: birth
        },
        success: function(data)
            {
                window.alert("success");
                $("#register_output").text(data);
            }

    });

    return false;
  });
});

在我的register.php文件中,我只是这样做:

$username = $_POST['username'];
$password = $_POST['pass'];
$gender = $_POST['gender'];
$birth_date = $_POST['birth'];

echo "$gender";

1 个答案:

答案 0 :(得分:4)

案件事项:

<label class="left">Gender: </label>
    <input type="radio" name="cc" value="M"> Male 
    <input type="radio" name="cc" value="F" checked="checked"> Female

cc!= CC

$('input[name="cc"]:checked').val();