gpt4 book ai didi

javascript - 使用表中的选定选项计算单元格值

转载 作者:行者123 更新时间:2023-11-30 15:00:00 25 4
gpt4 key购买 nike

我正在尝试构建一个包含我需要的所有定价选项的表格。这应该很简单,但我在包含计算的单元格中得到了 NaN 响应。

<!DOCTYPE html>
<html>
<body>
<table border="2">
<tr>
<th>Subscription Memberships</th>
</tr>
<tr>
<td>Subscription Type:
<select id="duration">
<option value="1MonthSub">Monthly Subscription</option>
<option value="3MonthSub">Quarterly Subscription</option>
<option value="6MonthSub">Bi-Annual Subscription</option>
<option value="yearSub">Yearly Subscription</option>
</select>
<br>
<input type="checkbox" id="discount">
I am eligible for the student, military, or senior discount.</td>
</tr>
<tr>
<td><span id="calculated"></span></td>
</tr>
</table>

<script>
function calcPrice() {

//Variables
var choice = document.getElementById("duration");
var dur = choice.options[choice.selectedIndex].text;
var price;
var per;
var output;

switch(dur) {
case "1MonthSub":
price = 65;
per = " / month";
break;
case "3MonthSub":
price = 220;
per = " / 3 months";
break;
case "6MonthSub":
price = 440;
per = " / 6 months";
break;
case "yearSub":
price = 900;
per = " / year";
break;
}//end switch

if (document.getElementById("discount").checked) {
price = price * 0.9;
}//end if

output = price + per;
return output;
}//end calcPrice()

document.getElementById("calculated").innerHTML = calcPrice();
</script>
</body>
</html>

NaN 单元格应该根据从下拉列表中选择的选项和复选框的真/假值计算价格。我试过移动脚本部分,当它们被放置在表格之前时,单元格中根本没有显示任何内容。我在这里缺少什么?

最佳答案

我改变了:

var dur = choice.options[choice.selectedIndex].text;

收件人:

var dur = choice.options[choice.selectedIndex].value;

<!DOCTYPE html>
<html>
<body>
<table border="2">
<tr>
<th>Subscription Memberships</th>
</tr>
<tr>
<td>Subscription Type:
<select id="duration">
<option value="1MonthSub">Monthly Subscription</option>
<option value="3MonthSub">Quarterly Subscription</option>
<option value="6MonthSub">Bi-Annual Subscription</option>
<option value="yearSub">Yearly Subscription</option>
</select>
<br>
<input type="checkbox" id="discount">
I am eligible for the student, military, or senior discount.</td>
</tr>
<tr>
<td><span id="calculated"></span></td>
</tr>
</table>

<script>
function calcPrice() {

//Variables
var choice = document.getElementById("duration");
var dur = choice.options[choice.selectedIndex].value;
var price;
var per;
var output;

switch(dur) {
case "1MonthSub":
price = 65;
per = " / month";
break;
case "3MonthSub":
price = 220;
per = " / 3 months";
break;
case "6MonthSub":
price = 440;
per = " / 6 months";
break;
case "yearSub":
price = 900;
per = " / year";
break;
}//end switch

if (document.getElementById("discount").checked) {
price = price * 0.9;
}//end if

output = price + per;
return output;
}//end calcPrice()

document.getElementById("calculated").innerHTML = calcPrice();
</script>
</body>
</html>

关于javascript - 使用表中的选定选项计算单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46696053/

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