在提交表單之前,我會檢查標題 (getElementById("title")) 的長度和附加檔案的數量 (("#filesTable_tr.template-download").length)。如果不行,我會阻止提交并顯示錯誤通知,之前將頁面滾動到有錯誤的元素,如下面的片段所示。
$(document).ready(function(){
$('#form').submit(function(e) {
if(document.getElementById("title").value.length < 2)
{
e.preventDefault();
$('html, body').animate({
scrollTop: $("input[name='rpagetitle']").offset().top-70
}, 600);
//here I show an error notifications
}
if($("#filesTable_ tr.template-download").length < 1)
{
e.preventDefault();
$('html, body').animate({
scrollTop: $("#dropzone").offset().top-70
}, 600);
//here I show an error notifications
}
{
return true;
}
});
});
但是,如果兩項檢查均失敗,則頁面滾動兩次,首先是標題,然后是 dropzone,如果第一次檢查,我如何防止第二次檢查 (("#filesTable_ tr.template-download").length < 1))失敗?所以如果標題不正確,滾動到它,顯示錯誤。如果標題正常,檢查檔案,如果不正常,滾動到它,顯示錯誤。如果兩者都OK,提交表格。
uj5u.com熱心網友回復:
使用回傳假;在您的第一個條件塊中,如果第一個案例為假,那么它只運行一個塊。
return 陳述句用于將特定值從函式回傳給函式呼叫者。當呼叫 return 陳述句時,該函式將停止執行。return 陳述句應該是函式中的最后一個陳述句,因為 return 陳述句之后的代碼將無法訪問。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/497874.html
標籤:jQuery
