Jquery基本表单验证

Jquery基本表单验证,jquery,validation,Jquery,Validation,我正在尝试创建自己的基本表单验证,而不必求助于繁重的、一刀切的插件,我已经编写了以下代码。我多久重写一次并重新开始似乎并不重要,我似乎无法让它工作 其思想是脚本检查表单以查看是否所有字段都已完成,如果已完成,则从submit按钮中删除disabled属性 职能:- function checkForm(){ $('#contact :input').each(function(){ if($(this).attr('value') == null){ var checked = f

我正在尝试创建自己的基本表单验证,而不必求助于繁重的、一刀切的插件,我已经编写了以下代码。我多久重写一次并重新开始似乎并不重要,我似乎无法让它工作

其思想是脚本检查表单以查看是否所有字段都已完成,如果已完成,则从submit按钮中删除disabled属性

职能:-

function checkForm(){
$('#contact :input').each(function(){
  if($(this).attr('value') == null){
     var checked = false; 
    } else {
     var checked = true;
    }
})
if (checked == true){
   alert('all filled in');
   //remove disabled attribute from button  
} else {
    alert('not completed');
    //add disabled attribute to button
}

}
以及调用该函数的代码:-

$('#contact :input').blur(function(){
     if ($(this).val() <= ''){
        $(this).next('.error').show();
    } else {
        $(this).next('.error').hide();
        checkForm();
    }
})
$('#contact:input').blur(函数(){

如果($(this).val(),则可以使用jquery.validate()函数进行此操作,其参考URL为:

更正:

function checkForm(){
$('#contact :input').each(function(){
 if($(this).val() == ''){
    var checked = false; 
  } else {
 var checked = true;
}
})
if (checked == true){
 alert('all filled in');
 //remove disabled attribute from button  
 } else {
alert('not completed');
//add disabled attribute to button
 }

}


由于您正在.each()的匿名函数内创建“checked”变量,因此在if(checked==true)测试中,该函数外的checked变量不可用(您会收到一个“checked is undefined”错误),这就是为什么不触发警报的原因

尝试首先在匿名函数外部定义“checked”变量,然后相应地更新它

function checkForm() {

    var checked = true;

    $('#contact :input').each(function () {
        if ($(this).val() == '') {
            checked = false;
        }
    })

    if (checked == true) {
        alert('all filled in');
        //remove disabled attribute from button  
    } else {
        alert('not completed');
        //add disabled attribute to button
    }

}

$('#contact :input').blur(function () {
    if ($(this).val() == '') {
        $(this).next('.error').show();
    } else {
        $(this).next('.error').hide();
        checkForm();
    }
})
下面是一个JSFIDLE示例

并调用该函数

$('#contact :input').on('blur', function(){
     if (!$.trim($(this).val()).length){
        $(this).next('.error').show();
    } else {
        $(this).next('.error').hide();
        checkForm();
    }
})

您比较
if($(this).val()的方式可能有问题,谢谢您的回答-我很确定我以前尝试过这种方法,但我只是再次尝试,它仍然不会触发函数底部的任何警报然后在$(this).val()上添加警报要检查您得到的值和您想要得到的值。还要检查您得到的值
checked
fieldDarn it,我之前做了“if(checked)”:)-这个示例似乎正在做我需要它做的事情-感谢您减少了代码。
function checkForm(){
  var checked = true;
  $('#contact :input').each(function(){
    if(!$.trim($(this).val()).length) checked = false;
  })
  if (checked){
   alert('all filled in');
   //remove disabled attribute from button  
  } else {
    alert('not completed');
    //add disabled attribute to button
  }
}
$('#contact :input').on('blur', function(){
     if (!$.trim($(this).val()).length){
        $(this).next('.error').show();
    } else {
        $(this).next('.error').hide();
        checkForm();
    }
})