gpt4 book ai didi

javascript - 计算2个单选按钮组的组合值

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

我是编码的新手,遇到了一个可能很简单的问题,但它让我卡住了。

我有两个单独的单选按钮组(每个 3 个按钮)。我已经为每个按钮分配了名称和值,并创建了一个 JS,它显示一个带有第一组分配值的警告框。这就是我被困的地方。

第二组按百分比保留标记,需要添加到第一组中选择的按钮的值。我需要更改什么?

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Help</title>
<script>
// Calculate and display the total cost of the selected
function calculateCost() {
var radiobutton; // A radio button
var cost = 0; // Cost of the selected room
// Add up the cost of the selected rooms
for (var i = 1; i <= 3; i++) {
radiobutton = document.getElementById("type" + i);
if (radiobutton.checked == true) {
cost += parseInt(radiobutton.value);
}
}
alert("The cost is $" + cost);
}
</script>
</head>
<body>
<form>
<h1>Market</h1>
<p>Complete the form below to calculate the cost.</p>
<p>Type:</p>
<p><input type="radio" id="type1" name="type"
value="135"> <label for="type1">A</label><br>
<input type="radio" id="type2" name="type"
value="175"> <label for="type2">B</label><br>
<input type="radio" id="type3" name="type"
value="215"> <label for="type3">C</label><br>
<p>Mark up:</p>
<p><input type="radio" id="mark1" name="mark"
value="0"> <label for="mark1">No Markup</label><br>
<input type="radio" id="mark2" name="mark"
value="15" ><label for="mark2">13% markup</label><br>
<input type="radio" id="mark3" name="mark"
value="15"> <label for="mark3">15% markup)</label><br>
<p><input type="submit" value="Submit"
onClick="calculateCost();">
<input type="reset"></p>
</form>
</body>
</html>

最佳答案

添加了一些没有重构的代码来添加百分比计算。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Help</title>
<script>
// Calculate and display the total cost of the selected
function calculateCost() {
var radiobutton; // A radio button
var markbutton;
var cost = 0; // Cost of the selected room
// Add up the cost of the selected rooms
for (var i = 1; i <= 3; i++) {
radiobutton = document.getElementById("type" + i);
if (radiobutton.checked) {
cost += parseInt(radiobutton.value);
}
markbutton = document.getElementById("mark" + i); // Get markup radio button
if (markbutton.checked) {
cost += (parseInt(radiobutton.value) * parseFloat(markbutton.value)) / 100; // Add percentage markup
}
}
alert("The cost is $" + cost);
}
</script>
</head>
<body>
<form>
<h1>Market</h1>
<p>Complete the form below to calculate the cost.</p>
<p>Type:</p>
<p><input type="radio" id="type1" name="type"
value="135"> <label for="type1">A</label><br>
<input type="radio" id="type2" name="type"
value="175"> <label for="type2">B</label><br>
<input type="radio" id="type3" name="type"
value="215"> <label for="type3">C</label><br>
<p>Mark up:</p>
<p><input type="radio" id="mark1" name="mark"
value="0"> <label for="mark1">No Markup</label><br>
<input type="radio" id="mark2" name="mark"
value="15" ><label for="mark2">13% markup</label><br>
<input type="radio" id="mark3" name="mark"
value="15"> <label for="mark3">15% markup)</label><br>
<p><input type="submit" value="Submit"
onClick="calculateCost();">
<input type="reset"></p>
</form>
</body>
</html>

关于javascript - 计算2个单选按钮组的组合值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32716257/

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