2013-07-03 4 views
0

jQuery 유효성 검사를 사용하여 입력이 완료되었는지 확인하는 양식이 있습니다. 그러나 무슨 일이 일어나고 있는지 검사하는 것은 validator가 메시지를 보여주고 있지만 Ajax가 boxesadd.php를 호출하고 있다는 것입니다. 나는이 ajax 호출이 성공적인 검증 후에 만 ​​만들어진 것임을 알게되었습니다. 입력이 완료되지 않은 경우 Ajax 이벤트까지 도달하는 이유는 무엇입니까? 당신이 당신의 메시지가 null이라고 아약스 호출하기 전에 감사입력이 유효한지 확인하기 전에 성공을 표시하는지 확인

jQuery 코드

$(function() { 

    $.validator.setDefaults ({ 

     errorClass: 'form_error', 
     errorElement: 'div' 
    }); 

    $("#BA_boxform").validate({ 
     rules: { 
      BA_customer: { 
       required: true 
      }, 
      customerdept: { 
       required: true 
      }, 
      customeraddress: { 
       required: true 
      }, 
      BA_service: { 
       required: true 
      }, 
      BA_box: { 
       required: true 
      }, 
      BA_destdate: { 
       required: true 
      }, 
      BA_authorised: { 
       required: true 
      } 
     }, 
     messages: { 
      BA_customer: { 
       required: '<br />* required: You must select a customer' 
      }, 
      customerdept: { 
       required: "<br />* required: You must select a department" 
      }, 
      customeraddress: { 
       required: "<br />* required: You must select a customer address" 
      }, 
      BA_service: { 
       required: "<br />* required: You must select a service level" 
      }, 
      BA_box: { 
       required: "<br />* required: You must enter a box number for intake" 
      }, 
      BA_destdate: { 
       required: "<br />* required: You must enter a destruction date" 
      }, 
      BA_authorised: { 
       required: "<br />* required" 
      } 
     } 
    }); 

    $('#BA_boxform').on('submit', function() { 

     var formdata = $('#BA_boxform').serialize() + '&submit=' + $(this).val(); 

     //alert(formdata); 
     $.ajax({ 
      type: "POST", 
      url: "/domain/admin/requests/boxes/boxesadd.php", 
      data: formdata, 
      dataType: 'json', 
      success: function (msg) { 
       //alert(msg); 
       if (typeof msg.boxerrortext !== "undefined" && msg.boxerrortext == "You must enter a box for intake") { 
        $("#BA_addbox").html(msg.boxerrortext); 
       } else { 
        $("#BA_addbox").html("You have successfully added box(es) " + '<span style="font-weight: bold;color:black;">' + msg.box + '</span>' + ' to the archive.' + '<br />' + 'You may now close this window or input more boxes.'); 
        $("#BA_boxform").get(0).reset(); 
       } 
       //$("#confirm_department").hide(); 

       /* 
       var $dialog = $('<div id="dialog"></div>') 
       .html('Your intake was successfully submitted and will be viewable in the reporting area.<br /><br />Thank you.'); 
       $dialog.dialog({ 
       autoOpen: true, 
       modal: true, 
       title: 'Box intake submission successfull', 
       width: 400, 
       height: 200, 
       draggable: false, 
       resizable: false, 
       buttons: { 
       Close: function() { 
       $(this).dialog("close"); 
       } 
       } 
       }); 
       */ 
       //alert(msg); 
       //console.log(msg); 
       //$("#BA_addbox").html(msg.box); 

       //$("#formImage .col_1 li").show(); 
       //$("#BA_boxform").get(0).reset(); 
       //$("#boxaddform").hide(); 
      } 
     }); 
     return false; 
    }); 
}); 
+0

100 % 확신 할 수는 없지만 유효한 지 확인하기 위해 전화를 걸지 않아도 되나요? – Liam

+0

@ Liam 유효성 검사를 설정하고 내 이해에 대한 YouTube 튜토리얼을 따랐지만, 유효한 것은 의무는 아니지만 살펴볼 것입니다. 덕분에 – user1532468

답변

0

는 확인.

+0

죄송 renish 더 – user1532468

+0

아약스 전화 전에 하나의 조건을 넣어, 만약 당신의 메시지 문자열이 null 또는 비어 있으면 당신은 그렇지 않으면 아약스 전화를해야합니다. –

+0

예를 설명하기 위해 작은 피들을 만들 수 있습니까? 감사 – user1532468

관련 문제