0

我试图让 Javascript 计算几个不同变量和结果的函数。老实说,我在过去的两天里一直在努力,实际上不知道从这里去哪里。我的代码如下:

<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,则作为示例,它将被除以1000
,如果选择b,则作为示例,它将被除以500
,如果选择c,则作为示例,它将被除以300。

但是..这些都不起作用。

我一直在一遍又一遍地检查代码并使其工作,尽管只是部分地因为事件然后显示两个弹出窗口。首先是选项值 1,然后在单击选项值 2 后替换它。

问题是:

在浏览器中查看并单击“计算付款”按钮后,没有任何反应。为什么是这样?


我希望我能说清楚,因为我对英语有点粗鲁,但我会很感激任何帮助。

4

6 回答 6

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);
        } 
于 2012-04-23T20:18:52.540 回答
0

1)下载萤火虫。或者在您使用的任何浏览器中打开 javascript 调试。它会显示你有一个错误。“什么都没有发生”的原因是处理在此错误后停止。document.monthly.payments[101]? 什么是101?不知道你来这里做什么。

2) 你有else(document.monthly.payments[103].style.visibility = "visible" )

你不能有其他(条件)。它应该只是 else {...}

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])  );
            }
于 2012-04-23T20:20:44.393 回答
0

您的 javascript 似乎没有任何意义,并且碰巧暴露了一些不太好的做法。听起来您只是想根据您在下拉菜单中选择的选项来提醒不同的值。在这种情况下,您最好插入这样的脚本:

<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>

这样,您可以以不显眼的方式确定单击事件的值,然后您可以根据需要提供代码来显示警报。

于 2012-04-23T20:21:21.550 回答
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>

​</p>

于 2012-04-23T20:28:24.030 回答
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;
于 2012-04-23T20:32:45.177 回答
0

这行得通 - 试试看。

<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>
于 2012-04-23T20:38:56.163 回答