通过函数减少重复传递许多对象

时间:2016-05-27 09:34:25

标签: javascript jquery

我正在检查用户输入并在提交时我正在检查输入是否为空。如果它是空的,我添加一个类,通过添加红色边框突出显示输入框为空。我通过一个名为app.setInvalidClass.init()的方法运行每个输入框来设置无效的类。我是否可以通过以下方式减少重复次数

app.setInvalidClass.init(!app.validation.email(userEmail), userEmail);
app.setInvalidClass.init(!app.validation.empty(userName), userName);
app.setInvalidClass.init(!app.validation.empty(userLastName), userLastName);
app.setInvalidClass.init(!app.validation.empty(contestName), contestName);
app.setInvalidClass.init(!app.validation.empty(countryName), countryName);
app.setInvalidClass.init(!app.validation.empty(date), date);
app.setInvalidClass.init(!app.validation.empty(month), month);
app.setInvalidClass.init(!app.validation.empty(year), year);

也许我可以使用Array#every,还是有更好的方法?

 $("#entry").submit(function( event ) {
                var userEmail = $("#email"),
                    userName = $("#first_name"),
                    userLastName = $("#last_name"),
                    date = $("#birth_day"),
                    month = $("#birth_month"),
                    year = $("#birth_year"),
                    countryName = $("#country"),
                    privacyCheck = $('#privacy_check');

                if (![date, month, year, countryName, userName, userLastName, userEmail]
                    .every(app.validation.empty) || !app.validation.dateOfBirth(date, month, year) || !app.validation.email(userEmail) || !privacyCheck.is(':checked') ) {
                    event.preventDefault();
                    alert("There are errors with your form.");
                    //set invalid class on form elements if errors
                    app.setInvalidClass.init(!app.validation.email(userEmail), userEmail);
                    app.setInvalidClass.init(!app.validation.empty(userName), userName);
                    app.setInvalidClass.init(!app.validation.empty(userLastName), userLastName);
                    app.setInvalidClass.init(!app.validation.empty(contestName), contestName);
                    app.setInvalidClass.init(!app.validation.empty(countryName), countryName);
                    app.setInvalidClass.init(!app.validation.empty(date), date);
                    app.setInvalidClass.init(!app.validation.empty(month), month);
                    app.setInvalidClass.init(!app.validation.empty(year), year);
                    privacyCheck.css({outline: 'red solid thin'})

                } else {
                    alert("You have entered the competition.");
                }      
            });


app.setInvalidClass = {
        init: function (invalidCondition, jQueryObject) {
            if(invalidCondition) {
                jQueryObject.addClass('invalid');
            }
            else {
                jQueryObject.removeClass('invalid');
            }
        }
    };

1 个答案:

答案 0 :(得分:1)

是的,对于那些相同的(所有empty s),您可以使用forEach

app.setInvalidClass.init(!app.validation.email(userEmail), userEmail);
[userName, userLastName, contestName, countryName, date, month, year].forEach(function(field) {
    app.setInvalidClass.init(!app.validation.empty(field), field);
});

也就是说,验证是一个丰富的领域,并且有libs /框架可以帮助你,你可能想看一下使用它而不是重新发明它。