2013-03-11 5 views
0

모든 입력이 채워 졌는지 확인하는 자바 스크립트가 포함 된 PHP 파일 (양식)이 있습니다. PHP 파일을 직접 볼 때 js가 완벽하게 작동하지만 다른 페이지에 PHP 파일을 포함하면 자바 스크립트가 더 이상 작동하지 않습니다.포함 된 PHP 파일의 자바 스크립트

내 자바 스크립트 코드 :

<script type="text/javascript" src="../js/modernizr.js"></script> 
<script type="text/javascript"> 
window.onload = function(){ 
    document.getElementById("topField").setAttribute("autocomplete","off"); 
    } 

window.onload = function() { 
    // get the form and its input elements 
    var form = document.forms[0], 
     inputs = form.elements; 
    // if no autofocus, put the focus in the first field 
    if (!Modernizr.input.autofocus) { 
     inputs[0].focus(); 
    } 
    // if required not supported, emulate it 
    if (!Modernizr.input.required) { 
     form.onsubmit = function() { 
      var required = [], att, val; 
      // loop through input elements looking for required 
      for (var i = 0; i < inputs.length; i++) { 
       att = inputs[i].getAttribute('required'); 
       // if required, get the value and trim whitespace 
       if (att != null) { 
        val = inputs[i].value; 
        // if the value is empty, add to required array 
        if (val.replace(/^\s+|\s+$/g, '') == '') { 
         required.push(inputs[i].name); 
        } 
       } 
      } 
      // show alert if required array contains any elements 
      if (required.length > 0) { 
       alert('The following fields are required: ' + 
        required.join(', ')); 
       // prevent the form from being submitted 
       return false; 
      } 
     }; 
    } 
} 

</script> 
+0

를? – datasage

+0

JS 콘솔에서 오류가 있는지 확인하십시오. –

+0

동일한 디렉토리 수준의 PHP 파일에 포함 시키시겠습니까? 스크립트 경로에주의하십시오. –

답변

0

이 나중에이 문제를 발견 한 사람에게 설명입니다. 자바 스크립트에서는 하나의 함수 만 이벤트 처리기에 연결할 수 있습니다. 더 연결하려면 체인을 연결해야합니다. 거의 모든 자바 스크립트 프레임 워크/라이브러리에는 이벤트 연결을 처리하는 일종의 메서드가 있습니다.

자바 스크립트를 사용하면 변수와 같은 함수를 처리 할 수 ​​있습니다. 따라서 이전 onload 함수를 변수에 할당 한 다음 나중에 새 onload 함수 내에서 호출 할 수 있습니다.

프레임 워크를 사용하지 않는 경우 이벤트 체인을 처리하기 위해 이와 같은 작업을 수행 할 수 있습니다.

function addLoadEvent(func) { 
    var oldonload = window.onload; 
    if (typeof window.onload != 'function') { 
    window.onload = func; 
    } else { 
    window.onload = function() { 
     if (oldonload) { 
     oldonload(); 
     } 
     func(); 
    } 
    } 
} 

다음에이 부를 것이다 : 오류 콘솔에서 볼 일을

addLoadEvent(function(){ 
    // Some code here 
}); 
관련 문제