2016-07-18 2 views
0
function validator(field_value, field) { 
      var name = document.getElementById('facility_Name').value; 
      var region = document.getElementById('facility_Region').value; 
      var state = document.getElementById('facility_State').value; 
      var county = document.getElementById('facility_County').value; 
      if (name == null || name == "") { 
       $("p#validator_error").append('<a href="#Facility_Details">Click here complete the <i>Name</i> field</a>'); 
       $('#validation_modal').modal('show'); 
       return false; 
      } 
      if (region == null || region == "") { 
       $("p#validator_error").append('<a href="#Facility_Details">Click here complete the <i>Region</i> field</a>'); 
       $('#validation_modal').modal('show'); 
       return false; 
      } 
      if (state == null || state == "") { 
       $("p#validator_error").append('<a href="#Facility_Details">Click here complete the <i>State</i> field</a>'); 
       $('#validation_modal').modal('show'); 
       return false; 
      } 
      if (county == null || county == "") { 
       $("p#validator_error").append('<a href="#Facility_Details">Click here complete the <i>County</i> field</a>'); 
       $('#validation_modal').modal('show'); 
       return false; 
      } 
      return true; 
     } 

     $('button#submitbutton').click(function() { 
      window.console.log('upload started'); 
      //Check for Facility Details 
      return validator(); 
// then do more stuff including an Ajax call 
}); 

이것은 간단한 검증 기능이어야합니까? 네 개의 필드 중 하나가 상자에 입력되지 않으면 매우 잘 작동합니다. 그건 문제가되지 않는 것 같습니다. 그러나 모든 필드가 입력되면 스크립트는 "return validator();"에서 중단됩니다. 어떤 종류의 오류도 발생시키지 않거나 스크립트의 나머지 부분을 완료하지 않습니다. "validator();"사용 어느 쪽도 작동하지 않습니까?Javascript return true not working/breaking script

+0

또는 오류가 발생. 콘솔 확인 했니? JSFiddle에서이 문제를 재현 할 수 있습니까? –

+0

왜 디버거를 넣고 단계를 밟아야합니까? –

+0

'function validator (field_value, field)'에서 매개 변수를 제거하십시오. – aldrin27

답변

0

"validator();"에서 스크립트가 중단됩니다. 그것이 존재하지 않기 때문에. 유효성 검사 절차를 위해 매개 변수가있는 함수를 정의했습니다. 함수에서 매개 변수를 제거하는 시도로 사용 : 그것은 실행해야 하나

if(validator()){// do stuff}

+0

좋은 전화, 나는 더 잘 알고 있어야 했음에 틀림 없다! – Chad