javascript - 带有单选标签的简单js计算器

标签 javascript html css

我找到了像我这样的主题,但我无法解决我的问题。所以我想通过对每个选项使用 if 语句而不使用 jquery 将价格乘以门票数量来计算单选票的成本。我不明白为什么我没有输出。看起来我的功能都不起作用,我也找不到原因。

你能帮帮我吗,我知道这很容易,但我还是个初学者

<!doctype html>
<html>
<head>
	<title>No Boundaries</title>
	<link rel="stylesheet" href="styles1.css">
	<script type="text/javascript">
		function validate(){
			if(!document.form1.cond.checked)
				{alert("Please accept the Terms and Conditions");
				return false;
			}
			if(document.form1.name.value.length < 2)
				{alert(“Please enter your full name, not just an initial”);
				return false;
			}
			return true;
		}
		function cost() {
			if (document.form1.accom["v"].checked) {
				var amount = parseInt(document.form1.amount.value);
				var ans = 90 * amount;
				document.form1.total1.value = ans;
			} 
			else if (document.form1.accom["t"].checked) {
				var amount = parseInt(document.form1.amount.value);
				var ans = 150 * amount;
				document.form1.total1.value = ans;
			} 
			else if (document.form1.accom["c"].checked) {
				var amount = parseInt(document.form1.amount.value);
				var ans = 45 * amount;
				document.form1.total1.value = ans;
			}
		}
		function post(){
			if(document.form1.del["1"].checked){
				var num = 0;
				var ans = num;
				document.form1.total2.value = ans;
			}
			if(document.form1.del["2"].checked){
				var num = 12;
				var ans = num;
				document.form1.total2.value = ans;
			}
			if(document.form1.del["3"].checked){
				var num = 16;
				var ans = num;
				document.form1.total2.value = ans;
			}
			if(document.form1.del["4"].checked){
				var num = 20;
				var ans = num;
				document.form1.total2.value = ans;
			}
		}
		function damage(){
			var total1 = parseInt(document.form1.total1.value);
			var total1 = parseInt(document.form1.total1.value);
			var ans = total1 + total2;
			document.form.total3.value = ans;
		}
	</script>

</head>

<body>
<section>
  <form name="form1">
		<fieldset>
			<legend>Personal details</legend>
			<div>
				Please enter your full name<br>
				<input name="name" type="text" required onsubmit="return validate();"><br>
			</div>
			<div>
				Address<br>
				<input name="addresss" type="text" required><br>
				<input type="text"><br>
				<input type="text"><br>
				<input type="text"><br>
			</div>
			<div>
				Phone Number<br>
				<input name="phone" type="tel"><br>
			</div>
			<div>
				Email Address<br>
				<input name="email" type="email" required><br>
			</div>
			<div>
				Date of Birth<br>
				<input name="birth" type="date"><br>
			</div>
		</fieldset>
		<fieldset>
			<legend>Ticket Details</legend>
				<div>
					Type of T<br>
					<label for="vil">V</label>
					<input type="radio" name="accom[]" value="v">
					<label for="town">T</label>
					<input type="radio" name="accom[]" value="t">
					<label for="con">C</label>
					<input type="radio" name="accom[]" value="c">
				</div>
				<div>
					Quantity of T
					<input name="amount" type="number" min="1" max="10" required><br>
					<br>
					<input type="button" name="cost" value="C C" onclick="cost();"><br>
				</div>
				<div>
					Delivery Options<br>
					<input type="radio" name="del[]" value="1" >Free<br>
					<input type="radio" name="del[]" value="2" >£12<br>
					<input type="radio" name="del[]" value="3" >£16<br>
					<input type="radio" name="del[]" value="4" >£20<br>
					<br>
					<input type="button" value="C D" onclick="post();"><br>
				</div>
		</fieldset>
		<fieldset>
			<legend>Terms And Conditions</legend>
			<input name="cond" type="checkbox" onsubmit="return validate();">
		</fieldset>
		<fieldset>
			<legend>Cost</legend>
			<input type="text" name="total1" readonly><br>
			<input type="text" name="total2" readonly><br>
			<input type="text" name="total3" readonly><br>
			<br>
			<input type="button" value="C F C" onclick="damage();"><br>
		</fieldset>
		<br><br>
		<fieldset>
			<input type="submit">
		</fieldset>
  </form>
</section>
</body>

</html>

最佳答案

成本函数应该是这样的,

function cost() {
    if (document.form1.accom.value.toLowerCase() == "v") {
        var amount = parseInt(document.form1.amount.value);
        var ans = 90 * amount;
        document.form1.total1.value = ans;
    } else if (document.form1.accom.value.toLowerCase() == "t") {
        var amount = parseInt(document.form1.amount.value);
        var ans = 150 * amount;
        document.form1.total1.value = ans;
    } else if (document.form1.accom.value.toLowerCase() == "c") {
        var amount = parseInt(document.form1.amount.value);
        var ans = 45 * amount;
        document.form1.total1.value = ans;
    }
}

并且为了让代码更加重构,就这样吧,

function cost() {
    var val = document.form1.accom.value.toLowerCase();
    var amount = parseInt(document.form1.amount.value);
    var ans;
    if (val == "v") {
        ans = 90 * amount;
    } else if (val == "t") {
        ans = 150 * amount;
    } else if (val == "c") {
        ans = 45 * amount;
    }
    document.form1.total1.value = ans;
}

关于javascript - 带有单选标签的简单js计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43221744/

相关文章:

javascript - 如何在高阶 javascript 函数中执行变量捕获?

javascript - 如何使用 onclick() 延迟加载列表项?

html - 暂停 HTML5 音频元素会导致整个文件被下载

javascript - 如何使用 html/javascript 在 Firefox 中禁用斜杠和撇号键弹出快速查找?

带有背景图片的 CSS Div - 如何允许在页面上扩展 div?

css - 如何在 slider 旋转上分层下拉菜单?

javascript - 有没有办法将实际的 DOM 写入字符串

javascript - 将 Nodejs 应用程序部署到 Azure 的最佳实践

html - 将纯文本添加到导航菜单

html - 如何使用垂直堆叠的 div 作为每个填充浏览器窗口的页面?