Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery.val()不适用于单选按钮_Jquery_Html - Fatal编程技术网

jQuery.val()不适用于单选按钮

jQuery.val()不适用于单选按钮,jquery,html,Jquery,Html,我有以下HTML代码: <form id="attorneyRegisterForm"> <div class="captchaIcons"> <label class="yourAgeLabel" for="rdo_1"> <input type="radio" class="yourAgeRadio"

我有以下HTML代码:

<form id="attorneyRegisterForm">
    <div class="captchaIcons">
        <label class="yourAgeLabel" for="rdo_1">
            <input 
                type="radio" 
                class="yourAgeRadio" 
                id="rdo_1" 
                name="attorneyRegister_age" 
                value="43b5b5755010e47acf5d549de05a205d" 
                title="Moon"
            />
            <i class="icon-moon ageIcons" title="Moon"></i>
        </label>
        ...
        <label class="yourAgeLabel" for="rdo_X">
            <input 
                type="radio" 
                class="yourAgeRadio" 
                id="rdo_X" 
                name="attorneyRegister_age" 
                value="c56f8bd55a15392e1039461c8006385c" 
                title="Flag"
            />
            <i class="icon-flag ageIcons" title="Flag"></i>
        </label>
    </div>
    <a href="#" id="registerAttorneyButton" class="button primary tiny">
        <?php
            _e('Register', 'z');
        ?>
    </a>
</form>
现在的问题是,当我从HTML表单中选择radio时,jQuery总是返回第一个radio值,而不是所选radio的值

我的javascript错了吗?或者我应该尝试其他方法来检索单选按钮吗?

您需要使用选择器


您可以使用以下jQuery获取名称组的选中单选按钮

 $("input[name='attorneyRegister_age']:checked").val();
试试这个,添加:签入jquery选择器

 $('.yourAgeRadio:checked').val();

删除JS中输入类型radio的id。改变

$yourAgeRadio               =   $('#attorneyRegisterForm input[type="radio"]');

样品


使用:选中选项以获取所选的单选按钮在选择器开头执行的律师注册已经是一种过度使用。此外,选择器将选择所有单选按钮。“我的选择器”将仅从“律师注册”年龄名称组中选择单选按钮。这将获取来自不同组类型的单选按钮的值,与我的相比,“我的选择器”仅获取“律师注册”年龄名称组的值。您将以这种方式创建错误,因为在名称属性选择器上没有使用双引号或转义单引号。您应该将名称选择器设置为[name=attorneyRegister\u age]或[name=\'attorneyRegister\u age\']。
 $('.yourAgeRadio:checked').val();
$yourAgeRadio               =   $('#attorneyRegisterForm input[type="radio"]');
$yourAgeRadio           =   $('input[type="radio"]:checked');
$(document).on('change','input[type="radio"]',function(){
    var $yourAgeRadio =   $('input[type="radio"]:checked');
   alert($yourAgeRadio.val()); 
});