jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 在每个输入之间迭代以验证值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个代码,如果为空,则验证输入的值,并使用twitter bootstrap popover显示消息错误

$(document).on("ready",login_events());

function login_events(){

    $("#userId,#password").on("change",validateEmpty);
    $("#loginButton").on("click",validateAll);

}

function validateEmpty(){

    input = $(this);
    input.popover({animation: true,placement: 'right',@R_696_10296@ger: 'manual',title: 'Field is Empty',content: input.attr("data-empty-message")});
    isValid = true;

    if(input.val() === ""){
        input.popover('show');
        isValid =  false;
    }
    else{
        input.popover('hide');
    }

    return isValid;

}

这在改变事件完成时非常有效,但我想做另一个函数,它将在表单中的每个输入之间进行迭代,验证它是否为空(validateEmpty())和preventDefault(如果有的话)为空.

正如您所看到的,我为#loginButton设置了click事件,以运行validateAll函数,我有类似的东西,但无法找到正确的方法

function validateAll(event){

    $("#loginForm input").each(validateEmpty);

}

解决方法

试试这个闭包:

function validateAll(event){
    var isValid = true;
    $("#loginForm input").each(function (){
        input = $(this);
        input.popover({animation: true,content: input.attr("data-empty-message")});
        isValid = true;

        if(input.val() === ""){
            input.popover('show');
            if (isValid)
                isValid = false;
        }
    });
    if (!isValid) {
        alert('form is not valid!');
    }
}

大佬总结

以上是大佬教程为你收集整理的jquery – 在每个输入之间迭代以验证值全部内容,希望文章能够帮你解决jquery – 在每个输入之间迭代以验证值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。