jQuery每个循环返回false不会结束函数 [英] jquery each loop return false not end the function

查看:70
本文介绍了jQuery每个循环返回false不会结束函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,该函数获取点分隔的字符串并将其解析为数组. 我想循环这些数组元素,并检查一个大于255的值,如果不继续执行函数语句,则返回false,并作为函数的结尾返回true.但是它永远不会停止循环..并始终返回true.

I have a function which get a dot seperated string and parse it to array. And I want to loop these array elements and check a value is greater than 255 and return false, if not continue to function statements and return true as end of function. But it never stops the loop.. and always return true.

这是代码:

checkipAddress = function(value){//instance value: 999.999.999.999 result:true
        debugger
        var array = value.split('.');
        $.each(array, function(index,val){
            debugger
            if(parseInt(val)>255)
                return false; // it should end the loop and exit function with return false.
        });
        return true;
    }

推荐答案

从一个函数返回并不能神奇地使调用它的函数结束,更不用说使用特定的返回值了.

Returning from one function doesn't magically make the one that called it end, much less use a specific return value.

如果要执行此操作,则必须设置外部函数将使用的变量:

If you want to do that, you have to set a variable that the outer function will use:

checkipAddress = function(value){
    var rv = true; // <=== Default return value
    var array = value.split('.');
    $.each(array, function(index,val){
        if(parseInt(val)>255)
            return rv = false; // <=== Assigns false to `rv` and returns it
    });
    return rv; // <=== Use rv here
}


侧面说明:您的函数将愉快地允许使用"0.-234343.-1.0""1foo.2foo.3foo.4foo"之类的IP字符串.您可能会考虑:


Side note: Your function will happily allow IP strings like "0.-234343.-1.0" and "1foo.2foo.3foo.4foo". You might consider:

checkipAddress = function(value){
    var rv = true; // <=== Default return value
    var array = value.split('.');
    $.each(array, function(index,str){
        str = $.trim(str);
        var val = +str;
        if(!str || val < 0 || val > 255)
            return rv = false; // <=== Assigns false to `rv` and returns it
    });
    return rv; // <=== Use rv here
}

更好,但是它也不会检查IP是否正好包含四个部分,而是允许使用"1.1e2.3.4"(指数表示法)之类的值.当然,所有这些都是特定于IPv4的,而我们正在进入IPv6的世界...

That's a bit better, but it also doesn't check whether there are exactly four parts to the IP, and allows values like "1.1e2.3.4" (exponent notation). And all of this is, of course, specific to IPv4, whereas we're entering an IPv6 world...

但是,如果您的目标是确保它是标准格式的四部分IPv4地址,那么我希望使用regex:

Sticking with IPv4, though, if your goal is to ensure that it's a four-part IPv4 address in normal form, I'd plump for regex:

checkipAddress = function(value){
    var rv;
    var match = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(value);
    var n, part;
    if (match) {
        rv = true;
        for (n = 1; rv && n < 5; ++n) {
            part = +match[n]; // We know it's not blank from the regex
            if (part > 255) { // We know it's not negative or fractional from the regex
                rv = false;
            }
        }
    } else {
        rv = false;
    }
    return rv;
}

或在现代浏览器上(或使用体面的Array#every填充程序):

Or on modern browsers (or using a decent Array#every shim):

checkipAddress = function(value){
    var match = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(value);
    rv = !match ? false : Array.prototype.slice.call(match, 1).every(function(entry) {
        // We know it's not blank, negative, or fractional from the regex
        return +entry <= 255;
    });
    return rv;
}

这篇关于jQuery每个循环返回false不会结束函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆