jQuery - Cancel form submit (using “return false”?)?

后端 未结 12 1656
盖世英雄少女心
盖世英雄少女心 2020-12-19 04:00

I\'m running into a bit of trouble while trying to cancel the submit of a form. I\'ve been following this tutorial (even though i\'m not making a login script), and it seems

12条回答
  •  长情又很酷
    2020-12-19 04:49

    I've run into similar issues. I solved them by removing the action and method of the form prior to validation and then adding them back in after validation. Here is the validator I wrote:

    var Validator = function (formSelector) {
        this.formSelector = formSelector;
    //  $(formSelector).submit(function() {return false;});
            this.Action = $(formSelector).attr("action");
            this.Method = $(formSelector).attr("method");
        $(formSelector).attr("action",function(){return ""}).attr("method",function(){return ""});  
            var donotsubmit = false;
            var notjustcheckbox = false;
            var checknotchecked = false;
        this.Required = new Array();
        this.Email = new Array();
        this.validate = function () {
            this.form = $(this.formSelector);
            var i = 0;
            for (i in this.Required){
                $(this.Required[i]).attr("value", function(index,attr){
                    // Check on checkboxes...   
                    if (this.getAttribute("type") == "checkbox"){
                        if (this.checked == false){ 
                            checknotchecked = true;
                            donotsubmit = true;
                        } else {
                        }       
                    } else {    
                        if (attr == "" || attr == undefined){   
                            this.style.border = "1px solid red";
                            notjustcheckbox = true;
                            donotsubmit = true;     
                        } else {
                            this.style.border = "1px solid green";
                        }
                    }
                });
            }
            i = 0;
            for (i in this.Email){
                $(this.Email[i]).attr("value", function(index,email){
                    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i ;
                    if (filter.test(email) == true || filter.test(email) == "true") {
                        this.style.border = "1px solid green";
                    } else if (filter.test(email) == false) {
                        donotsubmit = true;
                        notjustcheckbox = true;
                        this.style.border = "1px solid red";
                    }
                });
            }
            if (donotsubmit == true){
                if (checknotchecked == true && notjustcheckbox == true){    
                    alert("Please correct the fields in red and check the required checkboxes");
                } else if (checknotchecked == true && notjustcheckbox == false){
                    alert("Please check the required checkboxes");
                } else {
                    alert("Please correct the fields in red");
                }
                checknotchecked = false;
                notjustcheckbox = false;
                donotsubmit = false;
            } else {
                $(formSelector).attr({action : ''+this.Action+''});
                $(formSelector).attr({method : ''+this.Method+''});
                $(formSelector).submit();   
            }
            return true;
        };
    };
    

    You can implement it like this:

    $(document).ready(function(){
        var myForm = new Validator("#formId");
            myForm.Required = new Array("input.lightborder");
                    myForm.Email = new Array("#email");
                    $("#form-submit-button-id").click(function(){
                        myForm.validate();
                    });
    });
    

    You can add CSS Selectors for required fields. The one for Email must be unique.

提交回复
热议问题