编辑:回答,是一个语法错误,请参阅下面的固定码...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