简体   繁体   English

无法获取选中的单选按钮的值

[英]Can't get value of checked radio button

Here's the HTML: 这是HTML:

        <form>
            <div class="form-group">
                <div class="checkbox-detailed male_input">
                    <input type="radio" name="gender" id="male" value="male">
                    <label for="male">
                    <span class="checkbox-detailed-tbl">
                        <span class="checkbox-detailed-cell">
                            <span class="checkbox-detailed-title">Male</span>
                        </span>
                    </span>
                    </label>
                </div>
                <div class="checkbox-detailed female_input" style="margin-left: 25px!important">
                    <input type="radio" name="gender" id="female" value="female">
                    <label for="female">
                    <span class="checkbox-detailed-tbl">
                        <span class="checkbox-detailed-cell">
                            <span class="checkbox-detailed-title">Female</span>
                        </span>
                    </span>
                    </label>
                </div>
            </div>
            <button type="submit" class="btn btn-rounded" id="user_submit">Sign In</button>
        </form>

And Here's the javascript section: 这是javascript部分:

var user_gender = $("input[name='gender']:checked");
var user_submit = $('#user_submit');

user_submit.click(function(){
    console.log(user_gender.val())
});

I also tried var user_gender = $("input[name=gender]:checked"); 我也尝试了var user_gender = $("input[name=gender]:checked"); (without quotes between gender) and var user_gender = $("input:radio[name='gender']:checked"); (在性别之间没有引号)和var user_gender = $("input:radio[name='gender']:checked"); , it still didn't work. ,它仍然无法正常工作。 It's logging undefined for user_gender variable, am I doing anything wrong? 它记录的user_gender变量未定义,我做错了什么吗?

You are getting the checked radio when the page loads . 页面加载时,您将获得选中的单选按钮 You instead need to get it when the button is clicked . 相反, 单击按钮时需要获取它。

To do that move the selector inside the event handler: 为此,将选择器移到事件处理程序中:

$('form').submit(function(e) {
  e.preventDefault(); // stop the form submission. Remove once you've finished testing
  var $user_gender = $('input[name="gender"]:checked');
  console.log($user_gender.val())
});

A couple of things to note here. 这里需要注意几件事。 Firstly, the naming convention for variables which contain jQuery objects is to prefix their names with a $ as I've done in the above example. 首先,包含jQuery对象的变量的命名约定是像在上面的示例中所做的那样,以$作为前缀。

Secondly, when dealing with forms, hook to the submit event of the form itself, not the click event of the submit button. 其次,在处理表单时,请钩住form本身的submit事件,而不是submit按钮的click事件。 This is for accessibility reasons. 这是出于可访问性的原因。 It's also better semantically. 从语义上讲也更好。

You want to move the query within the function like this: 您想在函数内移动查询,如下所示:

var user_submit = $('#user_submit');

user_submit.click(function(){
    var user_gender = $("input[name='gender']:checked");
    console.log(user_gender.val())
});

What you are doing is getting the checked values before you are submitting, which is probably nothing at the time of loading the page. 您正在做的是在提交之前获取检查值,这在加载页面时可能什么都没有。

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

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