gpt4 book ai didi

javascript - 带有 javascript 的 HTML 表单在 IE 中获取 "NaN"但在 Chrome 中有效

转载 作者:搜寻专家 更新时间:2023-10-31 23:28:10 24 4
gpt4 key购买 nike

我绝不是程序员,但我设法让它在 Chrome 中运行,但当我在 IE 上尝试时我得到“NaN”(如果重要的话是 11)。它基本上需要 4 个标准并进行一些数学运算才能给出报价。有人可以帮忙吗?

<html>
<head>
<title>instant inspection quote tool</title>
<SCRIPT Language="JavaScript">

function calculateFee(frm){

var building = frm.building.value
var distance = frm.distance.value
var age = frm.age.value
var sqft = frm.sqft.value
var total = 0

building = Number(building)
distance = Number(distance)
age = Number(age)
sqft = Number(sqft)
total = Number(total)

total = building + distance + age + sqft
frm.total.value = total

}

</script>


</head>

<body>

<h1>Inspection Fee Calculator</h1>

<form method="post" action="">

Select the type of home
<br><input type="radio" name="building" value="375"> detached
<br><input type="radio" name="building" value="350"> semi-detached
<br><input type="radio" name="building" value="350"> condo or freehold townhome - end unit
<br><input type="radio" name="building" value="325"> condo or freehold townhome - interior unit
<br><input type="radio" name="building" value="299"> condo apartment

<br><br>Is the home within 50 kms of Ottawa?
<br><input type="radio" name="distance" value="0"> yes
<br><input type="radio" name="distance" value="25"> no

<br><br>Is the home less than 50 years old?
<br><input type="radio" name="age" value="0"> yes
<br><input type="radio" name="age" value="25"> no

<br><br>Is the home less than 2000 square feet?
<br><input type="radio" name="sqft" value="0"> yes
<br><input type="radio" name="sqft" value="25"> no
<br><br>

<input type="button" name="button" value="Calculate Fee" onClick="calculateFee(this.form)">

<table>
<tr>
<td align="right">Total Inspection fee:</td>
<td>
<input type="text" name="total" size="30" maxlength="30">
</td>
</tr>

</table>

</form>
</body>
</html>

最佳答案

试试这个 - 注意:querySelector 和 querySelectorAll 在 IE < 8 中不工作,在 8 中仅在标准模式下工作

Live Demo

function calculateFee(frm) {
var total = 0,
rads = frm.querySelectorAll('input:checked'); // get all checked radios
for (var i = 0; i < rads.length; i++) {
var num = Number(rads[i].value);
total += isNaN(num) ? 0 : num;
}
frm.total.value = total;
}

对于单个 radio 值,使用名称 - 但是如果 radio 未被选中,直接获取值将失败

var building = frm.querySelector("input[name=building]:checked");
if (building) total += Number(building.value);

为了验证,连接表单的提交

function validate(frm) {
var rads = frm.querySelectorAll('input:checked'); // get all checked radios
if (rads.length<4) {
alert("Please select one of each");
return false; // cancel form
}
return true;
}
window.onload=function() {
document.forms[0].onsubmit=function() {
return validate(this);
}
}

关于javascript - 带有 javascript 的 HTML 表单在 IE 中获取 "NaN"但在 Chrome 中有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25208699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com