1

jquery 検証を使用しdivたフォームがあり、フォームの上の同じ要素にすべてのエラーを表示したいと考えています。

私はこれをやろうとしましたが、うまくいきません:

$("#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;  
            }           
    });

私は何を間違っていますか?

4

1 に答える 1

2

ここに構文エラーがあります:

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!
                }
            },

if ブロックが 1 行であっても、常に中かっこを追加することをお勧めします。

于 2013-01-03T19:56:41.427 に答える