編輯:回答,是一個語法錯誤,請參閱下面的固定碼...jQuery的表單提交處理程序 - 古怪的行爲
大家好,很長一段時間的讀者,第一次提問者。
以下代碼是總是允許表單提交,即使考慮到我已經在函數結束時超出了任何條件語句之外的返回值。只有在函數體內沒有任何東西的情況下才允許表單提交,除非返回false。很奇怪。
我已經驗證該函數被調用的形式提交輸入函數體中的警報,並檢查它提醒了我。我嘗試將代碼剝離成離散函數,並將其指定爲事件處理程序,沒有區別。
的解決方法可能是監視我的控件的狀態和禁用/啓用表單的相應提交按鈕,但肯定我只是失去了一些東西明顯,因爲這種技術工作在其他地方。
有沒有人有任何想法?我的google-fu已經讓我失望了,所以我沒有看到任何可以看到的東西。我在智慧的結尾。順便說一句,順便說一句,是FF3,jQuery 1.4.2,我正在爲我公司的mantisbt安裝工作。表單的html完全正常。
乾杯,
摹
jQuery(document.forms[2]).submit(function(){
var canSubmit = true;
var msg = '';
// we only do validation if those controls are present and have some options which can be selected
if(jQuery("select[name=priority] option").length() > 0 && jQuery("select[name=severity] option").length() > 0){
//there must be a selection in priority
if(jQuery("select[name=priority]").val() == ''){
canSubmit = false;
msg = msg + 'Please select a priority!\n';
}
//there must be a selection in severity
if(jQuery("select[name=severity]").val() == ''){
canSubmit = false;
msg = msg + 'Please select a severity!\n';
}
//the priority cannot be P1 or P2 and also a feature request
if(jQuery("select[name=priority]").val() > 49
&& jQuery("select[name=severity]").val() == 60){
canSubmit = false;
msg = msg + 'Feature requests cannot be P1 or P2!';
}
//if there is some feedback, alert it to the user
if(msg != ''){
alert(msg);
}
}
//debugging override - always return false!
return false; //instead of canSubmit;
});
編輯:這裏的固定代碼。非常感謝加比!
var canSubmit = true;
var msg = '';
// validate form submission
jQuery(document.forms[2]).submit(function(e){
msg = '';
canSubmit = true;
// we only do validation if those controls are present and have some
// options
// which can be selected
if(jQuery("select[name=priority] option").length > 0 &&
jQuery("select[name=severity] option").length > 0){
// there must be a selection in priority
if(jQuery("select[name=priority]").val() == 0){
canSubmit = false;
msg = msg + 'Please select a priority!\n';
}
// there must be a selection in severity
if(jQuery("select[name=severity]").val() == 0){
canSubmit = false;
msg = msg + 'Please select a severity!\n';
}
// the priority cannot be P1 or P2 and also a feature request
if(jQuery("select[name=priority]").val() > 49
&& jQuery("select[name=severity]").val() == 60){
canSubmit = false;
msg = msg + 'Feature requests cannot be P1 or P2!';
}
if(!canSubmit){
// if there is some feedback, alert it to the user
if(msg != ''){
alert("There were some problems:\n" + msg);
}
return false;
}
}
});
你是'document.ready'處理器中運行呢? – 2010-11-12 12:29:36
是的,好吧。 document.ready確定正在查看哪個頁面並調用頁面特定的函數。此代碼位於其中一個函數中。我確定提交處理程序肯定正在執行。乾杯,G – dartacus 2010-11-12 14:02:44