我有这个代码,如果为空,则验证输入的值,并使用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', trigger: '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;
}
Run Code Online (Sandbox Code Playgroud)
这在改变事件完成时非常有效,但我想做另一个函数,它将在表单中的每个输入之间进行迭代,验证它是否为空(validateEmpty())和preventDefault(如果有的话)为空.
正如您所看到的,我为#loginButton设置了click事件,以运行validateAll函数,我有类似的东西,但无法找到正确的方法:
function validateAll(event){
$("#loginForm input").each(validateEmpty);
}
Run Code Online (Sandbox Code Playgroud)