0

我正在尝试通过(http://jorenrapini.com/blog/javascript/the-simple-quick-and-small-jquery-html-form-validation-solution)重新制作一个 jQuery 脚本。该脚本正在检查是否填写了 from ,如果没有填写,则会出现错误消息。

我想要做的是只在填写两个表单输入字段之一时收到错误消息,如果它们都没有,那么它们应该被忽略。表单字段被命名为“firstinput”和“secondinput”(您可以在代码中看到它们的 id)。

$(document).ready(function(){
    // Place ID's of all required fields here.
    required = ["firstinput", "secondinput"];
    // If using an ID other than #email or #error then replace it here
    email = $("#email");
    errornotice = $("#error");
    // The text to show up within a field when it is incorrect
    emptyerror = "Please fill out this field.";

    emailerror = "Please enter a valid e-mail.";

    $("#theform").submit(function(){    
        //Validate required fields
        for (i=0;i<required.length;i++) {
            var input = $('#'+required[i]);
            if ((input.val() == "") || (input.val() == emptyerror)) {
                input.addClass("needsfilled");
                input.val(emptyerror);
                errornotice.fadeIn(750);
            } else {
                input.removeClass("needsfilled");
            }
        }

        // Validate the e-mail.
        if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
            email.addClass("needsfilled");
            email.val(emailerror);
        }

        //if any inputs on the page have the class 'needsfilled' the form will not submit
        if ($(":input").hasClass("needsfilled")) {
            return false;
        } else {
            errornotice.hide();
            return true;
        }
    });

    // Clears any fields in the form when the user clicks on them
    $(":input").focus(function(){       
       if ($(this).hasClass("needsfilled") ) {
            $(this).val("");
            $(this).removeClass("needsfilled");
       }
    });
});

任何人都可以帮助我解决问题,我将非常感激。/一个花了很多时间解决这个问题但没有运气的女孩:(

4

1 回答 1

1

我会将您的 for 循环包装在一个条件中,该条件评估一个或另一个是否具有值。

if($("#field1").val() == "" && $("#field2").val() == ""){
//Ignore
}else{
//Do something
}

$(document).ready(function(){
    // Place ID's of all required fields here.
    required = ["firstinput", "secondinput"];
    // If using an ID other than #email or #error then replace it here
    email = $("#email");
    errornotice = $("#error");
    // The text to show up within a field when it is incorrect
    emptyerror = "Please fill out this field.";

    emailerror = "Please enter a valid e-mail.";

    $("#theform").submit(function(){    
        //Validate required fields
      if($("#firstinput").val() != "" || $("#secondinput").val() != "")
      {
        for (i=0;i<required.length;i++) {
            var input = $('#'+required[i]);
            if ((input.val() == "") || (input.val() == emptyerror)) {
                input.addClass("needsfilled");
                input.val(emptyerror);
                errornotice.fadeIn(750);
            } else {
                input.removeClass("needsfilled");
            }
        }
      }
        // Validate the e-mail.
        if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
            email.addClass("needsfilled");
            email.val(emailerror);
        }

        //if any inputs on the page have the class 'needsfilled' the form will not submit
        if ($(":input").hasClass("needsfilled")) {
            return false;
        } else {
            errornotice.hide();
            return true;
        }
    });

    // Clears any fields in the form when the user clicks on them
    $(":input").focus(function(){       
       if ($(this).hasClass("needsfilled") ) {
            $(this).val("");
            $(this).removeClass("needsfilled");
       }
    });
});
于 2012-11-15T18:03:26.490 回答