繁体   English   中英

无法使用jQuery获取单选按钮值

[英]Can't Get radio button value with jQuery

大家好,这是我的HTML代码:

 <div class="form_present_content">
        <span class="form_present_update">
            <fieldset class="form_present_fieldset" data-role="controlgroup" data-type="horizontal">
                <ul>
                    <li><input class="var_Amateur" name="TypeProfile[]" value="Amateur" id="Amateur" type="radio"><label for="Amateur">Amateur</label></li>
                    <li><input class="var_Professional" name="TypeProfile[]" value="Professional" id="Professional" type="radio"><label for="Professional">Professional</label></li>
                    <li><input class="var_Client" name="TypeProfile[]" value="Client" id="Client" type="radio"><label for="Client">Client</label></li>
                </ul>
            </fieldset>
        </span>
 </div>

我想获取选中的单选按钮的值,所以我该怎么做:

var var_TypeProfile = $('input[name="TypeProfile[]"]:checked', "#form_present_content").val();
alert(var_TypeProfile);

警报显示我未定义,需要您的帮助和感谢。

编辑jQuery代码:

var var_TypeProfile = $('input[name="TypeProfile[]"]:checked', ".form_present_content").val();
alert(var_TypeProfile);

在此编辑之后,警报不会出现。

, "#form_present_content")  --> id

应该

, ".form_present_content")  --> class

同样,在页面加载时,不会选中任何单选按钮。 因此,它始终是不确定的。

您需要编写一个change事件以在检查时获取值。

$('input[name="TypeProfile[]"]', '.form_present_content').on('change', function() {
    if($(this).is(':checked')) {
        alert($(this).val());
    }
});

检查小提琴

暂无
暂无

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

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