我试图让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点击后替换它。
问题是:
在浏览器中查看并点击“计算付款”按钮后,没有任何反应。这是为什么?
我希望我明白这一点,因为我对英语有点粗暴,但我会感激任何帮助。
答案 0 :(得分: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);
}
答案 1 :(得分:0)
1)下载萤火虫。或者在您使用的任何浏览器中打开javascript调试。它会显示您有错误。 “没有发生”的原因是处理在此错误后停止。 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]) );
}
答案 2 :(得分: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>
这样,您可以以不引人注目的方式确定click事件的值,然后您可以根据需要提供代码以显示警报。
答案 3 :(得分: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>
答案 4 :(得分: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;
答案 5 :(得分: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>