0

编辑:已回答,是语法错误,请参阅下面的固定代码...

大家好,老读者,第一次提问。

下面的代码始终允许表单提交,即使我在函数末尾覆盖了任何条件语句之外的返回值。仅当函数体中除了返回 false 之外没有任何内容时,才不允许提交表单。很奇怪。

我已经通过在函数正文中输入警报并检查它是否向我发出警报来验证该函数在表单提交时被调用。我尝试将代码剥离为离散函数并将其指定为事件处理程序,没有区别。

解决方法可能是监视我的控件的状态并相应地禁用/启用表单的提交按钮,但我肯定只是缺少一些明显的东西,因为这种技术在其他任何地方都适用。

有没有人有任何想法?我的 google-fu 让我失望了,所以我还没有看到任何东西。我无计可施。顺便说一句,这个上下文是 FF3,jQuery 1.4.2,我正在安装我公司的 mantisbt。表单的html是完全正常的。

干杯,

G

    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;

        }

    }

});
4

2 回答 2

1

它总是被提交,因为您发生了 javascript 错误,并且代码永远不会到达return false语句..

问题是 jquery 对象没有length()函数。它是一种财产。

您应该将两者更改为.length().length的第一个if.

或者,您可以使用该.size()方法。

有关它的更多信息,请访问http://api.jquery.com/length/

于 2010-11-12T14:09:41.103 回答
0

尝试添加“preventDefault()”...

jQuery(document.forms[2]).submit(function(evt){
    evt.preventDefault();
    .
    .
    .

然后,在您完成任何验证和操作后,您可以手动调用表单的提交:

$(document.forms[2]).submit();
于 2010-11-12T12:39:26.507 回答