Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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
Javascript jQuery可以';无法获取radiobutton正确的检查值_Javascript_Jquery - Fatal编程技术网

Javascript jQuery可以';无法获取radiobutton正确的检查值

Javascript jQuery可以';无法获取radiobutton正确的检查值,javascript,jquery,Javascript,Jquery,正如标题所说,我无法获取单选按钮的选中值,即使在调试器中我可以看到它,并且它已被选中 我显然做错了什么,但我不知道是什么。有什么帮助吗 谢谢 代码: 这是基本代码。与代码相结合应该不难 $('#element').click(function() { if($('#radio_button').is(':checked')) { alert("it's checked"); } }); 试试这个 function setupViewAll() { var compari

正如标题所说,我无法获取单选按钮的选中值,即使在调试器中我可以看到它,并且它已被选中

我显然做错了什么,但我不知道是什么。有什么帮助吗

谢谢

代码:


这是基本代码。与代码相结合应该不难

$('#element').click(function() {
   if($('#radio_button').is(':checked')) { alert("it's checked"); }
});
试试这个

function setupViewAll() {
        var comparisonCount = 0;
        var maxComparisonCount = 3;
        var $compareLabels = $('.compare-label');
        $compareLabels.addClass('compare-label-visible');

        // Check if the checkboxes are checked on load (in case the user navigates back), and if they are increase the check count.
        $compareLabels.each(function(index, label) {
            var radioButton = $(label).find('.radio-input');
            // Only show tooltip on unchecked checkboxes.
            if(radioButton.checked) {
                comparisonCount++;
                console.log(comparisonCount);
                checkSetToolTip($compareLabels, comparisonCount, maxComparisonCount);
            }
        });

        $('.infinit-view-all').click(function (e) {
            console.log("click in view all.");
            var $parent = $(e.target).parent();

            if ($parent.hasClass('.compare-label')) {
                var radioButton = $parent.find('.radio-input');
                console.log(radioButton);
                if (radioButton !== undefined) {
                    // If comparisonCount > maxComparisonCount, prevent click from bubbling up.
                      if($parent.find('.radio-input').is(':checked')) { alert("it's checked"); }
                    if (comparisonCount == maxComparisonCount) {
                        e.preventDefault();
                    }
                }
            }
        });

要提取该值,您必须选择所有具有相同名称的潜在单选按钮,并将其过滤到已检查的内容,即使用
:checked
。然后可以使用
.val()
获取特定单选按钮的值

jQuery(函数($){
$(“#检查”)。单击(函数(){
警报($('input[name=“the super radio”]:checked').val());
});
});

检查

在生产环境中,您可能应该进一步完善选择器,以确保页面上的多个表单具有相同名称的元素。

您可以在JSFIDLE中发布源代码吗?没有意义,它不起作用。它有更多的依赖性。我只是想知道如何找到复选框的选中值。您得到了哪些控制台错误?使用
radioButton[0]如何。选中,是@JCOC611检查的130倍,出于某种原因返回未定义。感谢您的建议。但是,.val()也返回为undefined=/您使用的是哪个版本的jQuery?我运行了这个$().jQuery,得到了1.11.1
function setupViewAll() {
        var comparisonCount = 0;
        var maxComparisonCount = 3;
        var $compareLabels = $('.compare-label');
        $compareLabels.addClass('compare-label-visible');

        // Check if the checkboxes are checked on load (in case the user navigates back), and if they are increase the check count.
        $compareLabels.each(function(index, label) {
            var radioButton = $(label).find('.radio-input');
            // Only show tooltip on unchecked checkboxes.
            if(radioButton.checked) {
                comparisonCount++;
                console.log(comparisonCount);
                checkSetToolTip($compareLabels, comparisonCount, maxComparisonCount);
            }
        });

        $('.infinit-view-all').click(function (e) {
            console.log("click in view all.");
            var $parent = $(e.target).parent();

            if ($parent.hasClass('.compare-label')) {
                var radioButton = $parent.find('.radio-input');
                console.log(radioButton);
                if (radioButton !== undefined) {
                    // If comparisonCount > maxComparisonCount, prevent click from bubbling up.
                      if($parent.find('.radio-input').is(':checked')) { alert("it's checked"); }
                    if (comparisonCount == maxComparisonCount) {
                        e.preventDefault();
                    }
                }
            }
        });