• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

在一个DIV元素中结合jquery验证错误

jquery 来源:Preston 7次浏览

我使用jquery validantion创建了一些表单,并且我想在表单上方显示相同的div元素中的所有错误。在一个DIV元素中结合jquery验证错误

我尝试这样做,但不工作:

$("#contactForm").validate({ 
      rules: { 
       assunto: { required: true }, 
       nome: { required: true, minlength: 2 }, 
       email: { required: true, email: true }, 
       telefone: { required: true }, 
       mensagem: { required: true } 
      }, 
      errorPlacement: function(error, element) { 
       if (element.attr("name") == "assunto" || element.attr("name") == "nome" || element.attr("name") == "email" || element.attr("name") == "telefone" || element.attr("name") == "mensagem") 
       //here i think i want to insert the location but i don't know how! 
      else 
       //here i think i want to insert the location but i don't know how! 
      }, 

      messages: { 
       assunto: {required: 'É necessário escolher um assunto'}, 
       nome: { required: 'É necessário preencher o campo nome', minlength: 'No mínimo 2 letras' }, 
       email: { required: 'Informe seu email', email: 'Informe um Email válido' },    
       telefone: { required: 'Informe seu Telefone' }, 
       mensagem: { required: 'Escreva sua Mensagem' } 
      }, 

      submitHandler: function(form){ 
       var dados = $(form).serialize(); 
       $.ajax({ 
        type: "POST", 
        url: "enviafaleconosco.asp", 
        data: dados, 
        success: function(data) 
        { 
         alert("Obrigado! Seu contato foi enviado com sucesso. Breve retornaremos!"); 
         $("#contactForm")[0].reset();     
        } 
       }); 
       return false; 
      }   
    }); 

什么,我做错了什么?


===========解决方案如下:

你有一个语法错误的位置:

errorPlacement: function(error, element) { 
       if (element.attr("name") == "assunto" || element.attr("name") == "nome" || element.attr("name") == "email" || element.attr("name") == "telefone" || element.attr("name") == "mensagem") 
       //here i think i want to insert the location but i don't know how! 
       else 
       //here i think i want to insert the location but i don't know how! 
      }, 

如果if和else没有里面什么也没有,你必须添加大括号{}关闭它们:

errorPlacement: function(error, element) { 
       if (element.attr("name") == "assunto" || element.attr("name") == "nome" || element.attr("name") == "email" || element.attr("name") == "telefone" || element.attr("name") == "mensagem"){ 
       //here i think i want to insert the location but i don't know how! 
       } else { 
       //here i think i want to insert the location but i don't know how! 
       } 
      }, 

这也是一个很好的做法,总是添加大括号,如果块,即使他们是单线


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)