javascript - 无法显示总费用
问题描述
我正在努力计算 totalPrice 和 finalPrice,我已经为此工作了几个小时。我尝试了一切,我应该将 if 和 else 语句放在脚本中吗?什么都试过了,还是不行,我的清除按钮也没有清除。谢谢你的帮助!!!
<html>
<head>
<meta charset = "UTF-8">
<title> World Travel </title>
<style>
body {background-color: black; font-family: optima;
color: white; text-align: left}
p { font-size: 20px}
button {font-size: 20px; background-color: white}
input { font-size: 20px}
</style>
</head>
<body>
<script>
var hotelNights= 0;
var travelPrice = 0;
var totalPrice = 0;
var finalPrice = 0;
</script>
<h1> World Travel </h1>
<h2> Price Processing </h2>
<p> Select Destination:
<input type = "radio" id = "a" name = "destination" size = "3"
onclick = /> Europe
<input type= "radio" id = "b" name= "destination" size = "3"
onclick = "destination == 'a';" /> Africa
</p>
<p> Select Air Travel Option:
<input type = "radio" id = "o" name = "airTravel" size = "3"
onclick = "
if (destination == "e") {
totalPrice += 500; }
else {
totalPrice += 700; }
" /> One Way
<input type= "radio" id = "x" name = "airTravel" size = "3"
onclick = "
if (destination == "e") {
totalPrice += 900; }
else {
totalPrice += 1300; }
" /> Round Trip
</p>
<p> Enter number of Hotel nights:
<input type = "text" id = "hotelNights" value = ""
onchange = "
if (destination == "e") {
totalPrice += 300*document.getElementaryById("hotelNights").value; }
else {
totalPrice += 200*document.getElementaryById("hotelNights").value; } " />
</p>
<p> Select Tour type:
<input type = "radio" id = "g" name = "tourPrice" size = "3"
onclick = "
if (desination == "e") {
totalPrice += 1500; }
else {
totalPrice + = 1300; } "/> Grand Package
<input type = "radio" id = "z" name = "tourPrice" size = "3"
onclick = "
if (destination == "e") {
totalPrice += 800; }
else {
totalPrice += 500; } " /> Basic Package
</p>
<p>
<button onclick="
finalPrice= (totalPrice*1.15);
totalPriceTextBox.value = '$' + totalPrice.toFixed(2);
finalPriceTextBox.value = '$' + finalPrice.toFixed(2);
"> Process </button>
</p>
<p> Total Price: $
<input type = "text" id = "totalPriceTextBox" value = "" readonly />
</p>
<p> Final Price (includes 15% surcharge): $
<input type = "text" id = "finalPriceTextBox" value = "" readonly />
</p>
<p>
<button onclick= "
a.checked = false;
hotelNights = '';
o.checked = false;
g.checked = false;
totalPriceTextBox.value = '';
finalPriceTextBox.value = '';
id_of_radio_button.checked = false;
id_of_text_box = '';
"> Clear </button>
</p>
<body>
</html>
..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... …………………………………………………………………………………………………………………………………………
解决方案
You can't use double quotes in an inline onclick handler:
onclick = "
if (destination == "e") { // Can't use double quotes here because you're already inside a set of double quotes!
totalPrice += 900; }
else {
totalPrice += 1300; }
" />
You also need to declare and initialize destination
.
Also, this onclick
looks wrong:
<input type = "radio" id = "a" name = "destination" size = "3"
onclick = /> Europe
推荐阅读
- postgresql - CentOS 7 pgbouncer.pid 权限问题
- resilience4j - 将 Prometheus 指标与 CircuitBreakerConfigCustomizer 一起使用
- jmeter - 如何管理客户端证书的大型密钥库以使用 JMeter 加载测试 Mutual TLS
- python - 用python中的字符串替换列表中的多个字符串并获取替换字符串出现的计数
- javascript - Android反应原生接近传感器
- python - sqlalchemy 连接烧瓶
- javascript - 获取请求后重定向需要两次点击?
- objective-c - TVOS 上的 contentOverlayView 未显示
- assembly - 如何在 x86 程序集中将 16 位寄存器移动到 8 位寄存器?
- regex - Delphi中用于StringList中所有匹配行的正则表达式