2014-11-11 4 views
-1

내가 누락 된 항목을 모르기 때문에 다음 코드로 도와주십시오. Compute 버튼을 클릭하면 기능이 트리거됩니다. computeAll(). 대신 스크립트는 내 행동에 반응하지 않습니다버튼이 이벤트를 트리거하지 않습니다.

HTML 코드 :

<!DOCTYPE html> 

<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title></title> 
     <script src="complex.js"></script> 

    </head> 
    <body> 
     <form name="calcul" method ="post"> 
     <table> 
      <tr><th></th><th>Real part</th><th>Imaginary part(i)</th></tr> 
      <tr><td>Enter the first complex number</td><td><input type="text" name="real1"/></td> 
      <td><input type="text" name="imaginary1"/></td></tr> 
      <tr><td>Enter the secound complex number</td><td><input type="text" name="real2"/></td> 
      <td><input type="text" name="imaginary2"/></td></tr> 

     <tr><td>Enter your choice</td><td colspan="2"><div><p>Addition<input type="radio" value="add" checked name="option"> 
     </p><p>Substraction<input type="radio" value="subs" name="option"></p></div> 
     </td></tr> 

     <tr><td >The answer is:</td><td><input type="text" name="displayReal"/></td><td><input type="text" name="displayImaginary"/></td></tr> 
     <tr><td colspan="3"></td></tr> 
      <tr> 
      <td><input type="button" value="Compute" onclick="computeAll()"></td> 
      <td><input type="button" value="Clear" onclick="clearAll()"></td> 
     </tr> 
     </table> 
     </form> 

    </body> 
</html> 

와 자바 스크립트 코드 <script> 태그가 위치한 것을

<script type="text/javascript"> 
    function computeAll() { 

     var val1, val2, img1, img2, resultR, resultI 
     val1 = parseInt(document.calcul.real1.value); 
     val2 = parseInt(document.calcul.real2.value); 
     img1 = parseInt(document.calcul.imaginary1.value); 
     img2 = parseInt(document.calcul.imaginary2.value); 
     if (calcul.option[0].checked){ 
      resultR = val1 + val2; 
     resultI = img1 + img2; 
     document.calcul.displayReal.value = resultR; 
     document.calcul.displayImaginary.value = resultI; 

     } 

     if (calcul.option[1].checked){ 
      resultR = val1 - val2; 
     resultI = img1 - img2; 
     document.calcul.displayReal.value = resultR; 
     document.calcul.displayImaginary.value = resultI; 
     } 
    } 
</script> 
+0

메소드를 호출하지 않고 메소드 내에서 오류가 발생했는지 디버그 했습니까? – brso05

+0

Chrome 또는 IE F12 윈도우와 같은 브라우저 개발 도구에서 먼저 코드를 테스트하십시오. –

+0

게시 한 자바 스크립트 코드가'complex.js'에 있습니까? –

답변

2

? <head>에서 참조 된 complex.js 파일에서? JavaScript 파일에 <script> 태그가 필요하지 않아서 문제를 일으킬 수 있습니다. JS 오류가 있는지 브라우저의 개발자 도구를 확인하십시오.

+0

감사합니다. – SocketM

1

외부 파일 (예 : <script src="complex.js"></script>)을 참조 할 때 <script type="text/javascript"></script> 안에 javascript 코드를 넣을 필요가 없습니다. 삭제하고 다시 시도하십시오.

+0

감사합니다 .-) 감사합니다. – SocketM

관련 문제