Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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 如果javascript中的条件不起作用_Jquery - Fatal编程技术网

Jquery 如果javascript中的条件不起作用

Jquery 如果javascript中的条件不起作用,jquery,Jquery,我在JS中有以下代码: if($('.log input:checked')) { alert('login checked'); alert('do what you want'); } else { alert('login not checked'); alert('you can not do what you want'); } 但它不能正常工作。IF-ELSE条件是否错误 if( $('.l

我在JS中有以下代码:

if($('.log input:checked')) 
    {
        alert('login checked');
        alert('do what you want');
    }
    else 
    {
    alert('login not checked');
        alert('you can not do what you want');
}
但它不能正常工作。IF-ELSE条件是否错误

if( $('.log input').is(':checked') ) { // true/false
您可以以jQuery方式使用(它是描述性的),并将针对匹配的元素返回一个布尔值。 但是您也可以使用JS
.length
属性

更好的解决方案(由@macek善意建议)


尽管使用
:checked
是测试是否选中单选按钮的首选方法,但您也可以简单地检查
$('.log input:checked')
是否有任何长度

if($('.log input:checked').length) {
    alert('login checked');
} else {
    alert('login not checked');
}
这与if条件完全不同,因为
length
将返回选中元素的数量(表示TRUE)或0(零)表示FALSE


在您的示例中,
if($('.log input:checked'))
将始终导致非错误结果,无论是否选中单选按钮,都会显示
login checked
消息。

您实际上应该为此使用
.prop()
。它将返回一个
true | false

if ( $('.log input').prop('checked') ) { ...
试试这个小插件:

插件:

(function($) {

    $.fn.eachChequed = function(fnc, fnnc) {

        var
            ck = $(this).filter('[type="checkbox"], [type="radio"]'),
            isFnc = $.isFunction(fnc),
            isFnnc = $.isFunction(fnnc);

        function callFnc(el) {
            if (isFnc) {
                return fnc.call(el);
            }
            return true
        }

        function callFnnc(el) {
            if (isFnnc) {
                return fnnc.call(el);
            }
            return true;
        }

        ck.each(function(i, el) {
            try {
                var r = $(el).is(':checked') ? callFnc(el) : callFnnc(el);
                return r;
            } catch (e) {}
        });
    };

})(jQuery);
运行:


测试:

太好了,正在运行,非常感谢。7分钟后,我将勾选您的答案;),因为我必须等待。jQuery已经为此
.prop()
。@macek在你留下评论后意识到“我怎么会错过它?!”):谢谢
(function($) {

    $.fn.eachChequed = function(fnc, fnnc) {

        var
            ck = $(this).filter('[type="checkbox"], [type="radio"]'),
            isFnc = $.isFunction(fnc),
            isFnnc = $.isFunction(fnnc);

        function callFnc(el) {
            if (isFnc) {
                return fnc.call(el);
            }
            return true
        }

        function callFnnc(el) {
            if (isFnnc) {
                return fnnc.call(el);
            }
            return true;
        }

        ck.each(function(i, el) {
            try {
                var r = $(el).is(':checked') ? callFnc(el) : callFnnc(el);
                return r;
            } catch (e) {}
        });
    };

})(jQuery);
$('input').eachChequed(function(){

    console.log('checked', $(this));

},function(){

    console.log('no checked', $(this));

});