2012-04-23 2 views
0

자바 스크립트가 여러 변수와 결과의 함수를 계산할 수있게하려고합니다. 솔직히 나는 지난 2 일을 그것에 썼다. 그리고 문자 그대로 여기에서 어디로 가야하는지 전혀 모른다. 내 코드는 다음과 같습니다 : I 이벤트를 위해 노력 해왔다자바 스크립트 함수가 변수를 계산하지 않습니다.

<html> 
<title>Calculate Your Monthly Payments</title> 
     <script language = "JavaScript"> 
      function Calc(){ 
      var a, res;    
      a = parseFloat(document.monthly.form.value); 
      res1 = a/1000; 
      res2 = a/500; 
      res3 = a/300; 

      if(document.monthly.payments[1].style.visibility = "visible") 
       window.alert("Your monthly repayments have been estimaded to be £" + res1); 

      else if(document.monthly.payments[2].style.visibility = "visible") 
       window.alert("Your monthly repayments have been estimaded to be £ " + res2); 

      else(document.monthly.payments[3].style.visibility = "visible") 
       window.alert("Your monthly repayments have been estimaded to be £ " + res3); 
      } 

     </script> 
</head> 


<div id="container"> 

     <h2>Calculate Your Monthly Payments</h2> 
     <h4>Please fill in the form below</h4> 
     <form name = "monthly"> 
      How much are you borrowing? (£): <br><input name = "form" type = "text" size = 10 id="textarea"> <br><br> 

       Mortgage Type: <br> 
       <select name = "payments"> 
        <option value="" selected>Please Select </option> 
        <option value="1">Mortgage 2.19% fixed until 2015 </option> 
        <option value="2">Mortgage 3.17% fixed until 2017 </option> 
        <option value="3">Mortgage 3.18% fixed until 2024 </option> 
       </select> 
       <br><br>     

      <input type = "button" value = "Calculate Payments! " onclick = "Calc();"> 
     </form> 
</div> 
</html> 



라는 팝업으로 표시되고 "당신의 월별 지불은 ...".

구현하려는 방정식은 입력 한 값이 선택한 값으로 나뉘어져 있어도 작동합니다.

(A)는 (B)가이 500
나눈 것 예를 들어 다음을 선택한 경우 및 C가 그것으로 나눈 것 예를 들어 다음을 선택한 경우 1000
나눈 것 예를 들어 다음을 선택하면 300.

그러나이 중 아무 것도 작동하지 않습니다.

나는 코드를 반복해서 작성하여 작동 시켰지만, 이벤트가 두 개의 팝업을 표시 할 때만 작동합니다. 첫 번째 옵션 값 1 인 및 두 번째 옵션 값 2

문제를있는 클릭 후 교체하는 것이 즉 : 브라우저에서보고하고 "계산 지불"을 클릭하면

버튼 아무 반응이 없습니다. 왜 이런거야?


영어로 약간 거칠기 때문에이 내용을 분명히 알았 으면 좋겠다.

+0

-1 실제 문제를 식별하지 못했습니다. –

+0

나는 이것이 좋은 생각이라고 생각하지 않는다 :'name = "form"'... 당신의 필드에'form'이라는 이름을 할당하는 것이 혼란 할뿐만 아니라,'form'은 제한된 단어 일 수 있습니다. – Sparky

답변

0
This works - try it 
<html> 
<title>Calculate Your Monthly Payments</title> 
     <script language = "JavaScript"> 
      function Calc(){ 
      var a, res;    
      a = parseFloat(document.monthly.form.value); 
      res1 = a/1000; 
      res2 = a/500; 
      res3 = a/300; 

      if(document.monthly.payments.value == "1") 
      { 
       window.alert("Your monthlys repayments have been estimaded to be £" + res1); 
      } 
      else if(document.monthly.payments.value == "2") 
      { 
       window.alert("Your monthly repayments have been estimaded to be £ " + res2); 
      } 
      else if(document.monthly.payments.value == "3") 
      { 
       window.alert("Your monthly repayments have been estimaded to be £ " + res3); 
      } 
     } 
     </script> 
</head> 


<div id="container"> 

     <h2>Calculate Your Monthly Payments</h2> 
     <h4>Please fill in the form below</h4> 
     <form name = "monthly"> 
      How much are you borrowing? (£): <br><input name = "form" type = "text" size = 10 id="textarea"> <br><br> 

       Mortgage Type: <br> 
       <select name = "payments"> 
        <option value="" selected>Please Select </option> 
        <option value="1">Mortgage 2.19% fixed until 2015 </option> 
        <option value="2">Mortgage 3.17% fixed until 2017 </option> 
        <option value="3">Mortgage 3.18% fixed until 2024 </option> 
       </select> 
       <br><br>     

      <input type = "button" value = "Calculate Payments! " onclick = "Calc();"> 
     </form> 
</div> 
</html> 

답변

로 표시하세요
+0

안녕 Jatin, 와우 나는 실제로 어떻게 놀랐다. 당신이 관리 할 수있는 원래의 스크립트의 대부분은 여전히 ​​고치기 만합니다. 나는 그걸 가지고 놀고 있었고, 가시성 함수 대신에 그것을 완전히 제거하기로 결정했습니다. 이것은 내가 찾고 있었던 스크립트입니다. 그럴 경우 네가 그럴 자격이 있다고 생각해. 많은 감사합니다! – Nikolas

+0

@Nikolas Thanks – Jatin

0

나는 코드의 다음 줄이 불을 지르고 다운로드) 지불 배열을 체크로

 if(document.monthly.payments[101].style.visibility = "visible") 
      window.alert("Your monthly repayments have been estimaded to be £" + res1); 

     else if(document.monthly.payments[102].style.visibility = "visible") 
      window.alert("Your monthly repayments have been estimaded to be £ " + res2); 

     else(document.monthly.payments[103].style.visibility = "visible") 
      window.alert("Your monthly repayments have been estimaded to be £ " + res3); 

확인이 아웃

 if(document.monthly.payments.style.visibility = "visible") 
      window.alert("Your monthly repayments have been estimaded to be £" + res1); 

     else if(document.monthly.payments.style.visibility = "visible") 
      window.alert("Your monthly repayments have been estimaded to be £ " + res2); 

     else(document.monthly.payments.style.visibility = "visible") 
      window.alert("Your monthly repayments have been estimaded to be £ " + res3); 
     } 
+0

안녕하세요. 고마워요, 저는 그걸 보았습니다. 실제로 배열이 문제라고 생각해서 실제로 그걸로 놀고 있었기 때문에 실제로 1,2,103이 아니기 때문에 101,102,103이 아닙니다. 하지만 옙 나는 변화를 만들었고 불운하게도 여전히 운이 없다. ( – Nikolas

0

일을보고 할 필요가 있다고 생각합니다. 또는 사용중인 브라우저에서 자바 스크립트 디버깅을 사용하도록 설정하십시오. 오류가 있음을 보여줍니다. "아무 일도 일어나지 않는"이유는이 오류 후에 처리가 중지되기 때문입니다. document.monthly.payments[101]? 101은 무엇입니까? 네가 여기서 뭘 하려는지 확신 할 수 없어.

2) 당신은) 당신이 다른 (조건을 가질 수 없습니다 else(document.monthly.payments[103].style.visibility = "visible")

있습니다. 그냥 다른 있어야한다 {...}

3)이 시도 :

if(document.monthly.payments.selectedIndex == 1) 
       window.alert("Your monthly repayments have been estimaded to be £" + res1); 

      else if(document.monthly.payments.selectedIndex == 2) 
       window.alert("Your monthly repayments have been estimaded to be £ " + res2); 

      else if(document.monthly.payments.selectedIndex == 3) 
       window.alert("Your monthly repayments have been estimaded to be £ " + res3); 
      } 

함수는 다음과 같을 수 있습니다 :

function Calc(){ 
      if (document.monthly.payments.selectedIndex == 0){ 
       alert("Please select a mortgage type"); 
       return; 
      } 

      var mortgages = [1000,500,300]; 
      window.alert("Your monthly repayments have been estimaded to be £" + (parseFloat(document.monthly.form.value)/mortgages[document.monthly.payments.selectedIndex-1]) ); 
      } 
+0

안녕하세요, 답장을 보내 주셔서 감사합니다. 사실 저는 dreamweaver에서이 작업을 수행하고 있으며 Chrome 19 및 FireFox 10의 코드를 테스트하고 있습니다. 감사합니다. 감사합니다. – Nikolas

+0

@Nikolas, Chrome에서 F12를 누르면 콘솔에 오류가 표시됩니다. – aquinas

0
이해가 보인다 나던

자바 스크립트, 좋지 않은 습관을 폭로하게됩니다. 드롭 다운에서 선택한 옵션에 따라 다른 값을 경고하기 만하면됩니다.이 경우, 당신은 더 나은이 같은 스크립트를 삽입 제공 될 것이다 :

<select id="mySelect"> 
    <!-- Options --> 
</select> 

<script> 
    var selectElement = document.getElementById('mySelect'); 

    if(selectElement != null) 
    { 
     selectElement.addEventListener('change', function() 
     { 
      var selectedOptionValue = this.options[this.selectedIndex].value; 

      switch(selectedOptionValue) 
      { 
       case 1: // Do Stuff 
        break; 
       case 2: // Do Stuff 
        break; 
       default: // Do Stuff 
        break; 
      } 
     }); 
    } 
</script> 

이 방법, 당신은 눈에 거슬리지 방법으로 클릭 이벤트에 값을 결정을하고,을 보여주기 위해 필요에 따라 다음 코드를 제공 할 수 있습니다 경보.

0

jsFiddle (http://jsfiddle.net/DTTg5/6/)

<script type="text/javascript"> 
    function calculate() 
    { 
     var payments = parseInt(document.getElementById("payments").value); 
     var a = parseFloat(document.getElementById("borrowing").value); 

     var res1 = a/1000; 
     var res2 = a/500; 
     var res3 = a/300; 

     if (payments == 1) 
     { 
      alert("Your monthly repayments have been estimaded to be £" + res1); 
     } 
     else if (payments == 2) 
     { 
      alert("Your monthly repayments have been estimaded to be £ " + res2); 
     } 
     else if (payments == 3) 
     { 
      alert("Your monthly repayments have been estimaded to be £ " + res3); 
     } 
    } 

</script> 

<form name = "monthly"> 
    How much are you borrowing? (£): 
    <br/> 
    <input name="form" type="text" size="10" id="borrowing"/> 
    <br/><br/> 
    Mortgage Type: 
    <br/> 
    <select name="payments" id="payments"> 
     <option value="0" selected>Please Select </option> 
     <option value="1">Mortgage 2.19% fixed until 2015 </option> 
     <option value="2">Mortgage 3.17% fixed until 2017 </option> 
     <option value="3">Mortgage 3.18% fixed until 2024 </option> 
    </select> 
    <br/><br/>     
    <input type="button" value="Calculate Payments!" onclick="calculate();"> 
</form> 

0
 var a, res;    
     a = parseFloat(document.monthly.form.value); 
     res1 = a/1000; 
     res2 = a/500; 
     res3 = a/300; 

더 나은 방어 코딩 :

 var a;    //declare only one variable on a line 
     a = parseFloat(document.getElementById("borrowing").value) ; 
     if (isNaN(a)) { 
      alert("please enter nn.nn"); 
      return; 
     } 
     var res1 = a/1000; 
     var res2 = a/500; 
     var res3 = a/300; 
+0

안녕하세요 David, 감사합니다. 나는 이것에 익숙하지 않았지만 나는 그것을 시험해 보았고 내가 이전에 가지고 있었던 것에 그것을 선호했다. 나는 장래의 사용을 위해 그것을 확실히 녹음 할 것이다. 정말로 감사 하노라 . – Nikolas

관련 문제