2012-04-03 43 views
0

我需要一些幫助!Javascript/jQuery返回false不會停止執行

我有一個代碼塊,允許表單提交前檢查某些元素,循環通過OK。

我會預料到,或者至少希望第一次發現問題時,顯示警告,然後停止執行。實際發生的情況是警報會多次顯示,直到最後一個,而最後一個實際返回false!

我不明白這是爲什麼,我做錯了什麼? JavaScript在下面。

在此先感謝!

$('#deliverInForm').submit(function(){ 
    $('.childRow').each(function(){ 
     if($(this).find('.thisBinName').val()!='' || $(this).find('.unitQty').val()!=''){ 
      if($(this).find('.thisBinName').val()==''){ 
       alert('You have entered a quantity but no Bin, please correct and try again!'); 
       return false; 
      }else if($(this).find('.unitQty').val()==''){ 
       alert('You have entered a Bin but no quantity, please correct and try again!'); 
       return false; 
      }else if($(this).find('.unitQty').val()==0){ 
       alert('Can\'t move zero units into a bay, please correct and try again!'); 
       return false; 
      }else if($(this).find('.unitQty').val()<0){ 
       alert('Can\'t deliver in minus units into a bay, please correct and try again!'); 
       return false; 
      } 
     } 
    }); 

    if($('#supDocNo').val()==''){ 
     alert('Can\'t leave Supplier Reference blank, please correct and try again!'); 
     return false; 
    } 
    return true; 
}); 

回答

3

讓你的功能需要一個事件參數,然後調用event.preventDefault()

例如

$('#deliverInForm').submit(function(event){ 
$('.childRow').each(function(){ 
    if($(this).find('.thisBinName').val()!='' || $(this).find('.unitQty').val()!=''){ 
     if($(this).find('.thisBinName').val()==''){ 
      alert('You have entered a quantity but no Bin, please correct and try again!'); 
      event.preventDefault(); 
      return false; 
     }else if($(this).find('.unitQty').val()==''){ 
      alert('You have entered a Bin but no quantity, please correct and try again!'); 
      event.preventDefault(); 
      return false; 
     }else if($(this).find('.unitQty').val()==0){ 
      alert('Can\'t move zero units into a bay, please correct and try again!'); 
      event.preventDefault(); 
      return false; 
     }else if($(this).find('.unitQty').val()<0){ 
      alert('Can\'t deliver in minus units into a bay, please correct and try again!'); 
      event.preventDefault(); 
      return false; 
     } 
    } 
}); 

if($('#supDocNo').val()==''){ 
    alert('Can\'t leave Supplier Reference blank, please correct and try again!'); 
    event.preventDefault(); 
    return false; 
} 
return true; 
}); 
+0

這個方法的問題是,最終的檢查('如果($('#supDoc ...')仍然會執行,可能導致多個警報。 – sje397 2012-04-03 13:14:18

+0

簡單解決e,而不是警告錯誤,將警報消息放入變量中,檢查變量是否爲空,如果不是,則警告其內容。或者在foreach之前放置第一個提醒;) – 2012-04-03 13:17:47

1

的問題是,你的each循環裏面,return false剛剛離開你傳遞給each功能。根據docs,這將退出each循環,但它不會退出您的提交處理程序。

你可以做一些醜陋這樣的:

$('#deliverInForm').submit(function(){ 
    var exit = false; 
    $('.childRow').each(function(){ 
    if($(this).find('.thisBinName').val()!='' || $(this).find('.unitQty').val()!=''){ 
     if($(this).find('.thisBinName').val()==''){ 
     alert('You have entered a quantity but no Bin, please correct and try again!'); 
     exit = true; // <----------------set flag 
     return false; 
     //... 
    }); 

    if(exit) return false; 
    // ... 
}); 
+0

謝謝!幫助我解決同一類問題 – 2014-09-15 21:32:56

0

jquery each documentation

我們可以通過將回調函數返回false打破在一個特定的迭代。每個$()循環。返回非錯誤與for循環中的continue語句相同;它會立即跳到下一個迭代。

我認爲你必須把你的回報虛假的每個循環,而不是查找()

0

裏面試試這個:

$('#deliverInForm').submit(function(){ 
    var errLog = ''; 
    $('.childRow').each(function(){ 
     if($(this).find('.thisBinName').val()!='' || $(this).find('.unitQty').val()!=''){ 
      if($(this).find('.thisBinName').val()==''){ 
       errLog += 'You have entered a quantity but no Bin, please correct and try again!\n'; 
      } 
      if($(this).find('.unitQty').val()==''){ 
       errLog += 'You have entered a Bin but no quantity, please correct and try again!\n'; 
      } 
      if($(this).find('.unitQty').val()==0){ 
       errLog += 'Can\'t move zero units into a bay, please correct and try again!'; 
      } 
      if($(this).find('.unitQty').val()<0){ 
       errLog += 'Can\'t deliver in minus units into a bay, please correct and try again!'; 
      } 
     } 
    }); 

    if($('#supDocNo').val()==''){ 
     errLog += 'Can\'t leave Supplier Reference blank, please correct and try again!\n'; 
    } 

    if(errLog != '') { 
     alert(errLog); 
     errLog = ''; 
     return false; 
    } 
    return true; 
}); 

希望它能幫助。

0

事件處理函數需要返回值。

$(".link").submit(function(){ 
    return false; 
}); 

要顯示的第一個錯誤對話框,並返回false,你必須做這樣的事情......

$(".link").submit(function(){ 
    var submit=true; 
    $("div").each(function(){ 
     if(submit) // comment this out to show all dialogs 
      if(!condition){ 
       submit = false; 
       alert("problem"); 
       return ""; // return here is irrelevant 

      } 
    }) 
    return submit; 
}); 

一件事,如果你的函數拋出一個錯誤就不會返回false和提交反正會發生......

$(".link").submit(function(){ 
    throw new Error("foo"); 
    return false; // never happens form submits as if returning true; 
}); 
相關問題