jquery each 跳出及返回問題,jqueryeach

來源:互聯網
上載者:User

jquery each 跳出及返回問題,jqueryeach
無意中用到jquery 中的each,來源程式是這樣寫的

function verify(){    $("input").each(function (i,n){         var search_eml = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;        var isMobile=/^(?:13\d|15\d)\d{5}(\d{3}|\*{3})$/;           var isPhone=/^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;        if($(this).val().trim()==''){            alert($(this).attr("placeholder")+"不可為空");            return false;        }        if($(this).attr("id")=='tel'){            if($(this).val().trim().length!=11){                       alert("請在'"+$(this).attr("placeholder")+"'輸入正確格式 !");                $(this).focus();                return false;            }        }        if($(this).attr("id")=='email'){            if(!search_eml.test($(this).val())){                       alert("請在'"+$(this).attr("placeholder")+"'輸入正確格式 !");                $(this).focus();                return false;            }        }        })        return true;    }}

原本以為會在調用處返回true或false但是結果是在調用處沒有返回任何東西,搜尋關於each的資料時發現  
break; 等同於  return false;
continue; 等同於 return true;
也就是說 return ture 或 return false 表示的是當前迴圈是否跳出,是否繼續,所以做出如下修改.
function verify(){    var v = '1';    $("input").each(function (i,n){         var search_eml = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;        var isMobile=/^(?:13\d|15\d)\d{5}(\d{3}|\*{3})$/;           var isPhone=/^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;        if($(this).val().trim()==''){            alert($(this).attr("placeholder")+"不可為空");            v = '0';            return false;        }        if($(this).attr("id")=='tel'){            if($(this).val().trim().length!=11){                       alert("請在'"+$(this).attr("placeholder")+"'輸入正確格式 !");                $(this).focus();                v = '0';                return false;            }        }        if($(this).attr("id")=='email'){            if(!search_eml.test($(this).val())){                       alert("請在'"+$(this).attr("placeholder")+"'輸入正確格式 !");                $(this).focus();                v = '0';                return false;            }        }        if(v=='0'){            return false        }        })    if(v=='0'){    return false    }else{    return true;    }}

在jquery each 中想要跳出當前迴圈(break)需要使用 return false,想要執行下一個(continue)使用return true;所以在你在each的內建函數中寫return false時是不會返回調用函數處的.

jquery 的each,如果想break,那使用return false;就行,但是問題也來了

jq 的return false;只是跳出迴圈而已~你要傳回值還是要在迴圈外面做呀~
code:
function test(){
var success = false;
$(..).each(function () {
if (..) {
success = true;
return false;
}
});
return success ;
}
jquery是對象鏈~所以$(..).each()返回的還是對象集合~each(function(){})~只是回呼函數而已~所以在回呼函數裡不能返回結果到each外面~
 
jquery裡面怎跳出each迴圈

each函數是一個多執行函數,它會把所有匹配到的元素都執行一次。第一,如果退出當前執行,請用return。第二種,如果要停止運行到剩下未執行,那麼我還沒有找到方法,建議在前面的選取器上做好範圍限制,避免這種情況的發生。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.