最新消息: 新版网站上线了!!!

javascript函数中使用jquery的each遍历dom如何跳出each和函数

jquery中的each

返回 'false' 将停止循环 (就像在普通的循环中使用 'break')。

返回 'true' 跳至下一个循环(就像在普通的循环中使用'continue')。


    <script type="text/javascript">
        function check() {
            var isFlag = true;
            $("input[name='res_desc[]']").each(function () {
                if ($(this).val().length>60){
                    alert("配置描述不能大于60个字符:"+$(this).val());  //$(this)循环到的当前文本框
                    isFlag = false;//赋值为false,等退出each,进入函数之后,根据此进行判断退出函数
                    return false;   //结束each,退出each循环
                }
            });
            if(!isFlag){
                return false;   //结束function
            }
        }
    </script>


    <form method="post" action="****/resAdd.php?act=addHandle" onsubmit="return check()">
    <input type="text" required="" name="res_desc[]" value="">
    <input type="text" required="" name="res_desc[]" value="">
    <input type="text" required="" name="res_desc[]" value="">
    <input class="tableBtn" value="创建" type="submit">
    <input class="tableBtn cancelButton" value="取消" type="reset">
    </form>


转载请注明:谷谷点程序 » javascript函数中使用jquery的each遍历dom如何跳出each和函数