javascript - 单选按钮值在计算中不起作用

标签 javascript html css

我正在尝试获取 bat mobile、gauss hog 和 light cycle 的单选按钮来计算成本。其他计算适用于其他变量但不适用于 radCar。

    <!DOCTYPE html>
    <html lang="en">
    <head>
<meta charset="utf-8">
<title>Cars4You Car Rentals</title>
<link rel="stylesheet" type="text/css" href="mystylesheet.css">
    <script type="text/javascript">
function calculateCost()
{
var cost = 0
var radCar;
var chkGPS = 0
var rentalDay = parseInt(document.rentalForm.rentalDay.value);
var insurance = 0

radCar = document.rentalForm.radCar.value

    if (radCar=="batmobile")
{
cost = 100
}
if (radCar=="gausshog")
{
cost = 85
}
if (radCar=="lightcycle")
{
    cost = 40
} 

if ( document.rentalForm.chkGPS.checked )
{
    chkGPS = chkGPS+5;
}

if ( document.rentalForm.insurance.value=="Yes" )
{
insurance = insurance+20;
}

if (document.rentalForm.rentalDay.value>=7)
{
cost = cost-10;
}

finalPrice = (cost + chkGPS)*rentalDay + insurance;
alert("Your rental cost is $" + finalPrice);
}
    </script>
    </head>
    <body>
<div id="header">
    <h1>Cars4You Car Rentals</h1>
    <a href="home.html" target="_blank">Home</a>
    <----------------------------------------------------->
    <a href="contact.html" target="_blank">Contact</a>  
</div>
<div id="main_content">
    <form name=rentalForm>
First Name: 
    <input type="text" name="txtFirstName"> <br>
Last Name: 
    <input type="text" name="txtLastName"> <br>
    <h2>Vehicle Type</h2>
Batmobile ($100/day) : 
    <input type="radio" name="radCar" value="batmobile"> <br>
Gausshog ($85/day) : 
    <input type="radio" name="radCar" value="gausshog"> <br>
Lightcycle ($40/day) : 
    <input type="radio" name="radCar" value="lightcycle"> <br><br>
How many rental days?
    <select name="rentalDay">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option> <br>
    </select> <br>
*$10 discount for 7 or more days <br>
    <h2>Extras</h2>
Insurance ($20)?
    <select name="insurance">
        <option value="Yes">Yes</option>
        <option value="No">No</option>
    </select> <br>
GPS: 
    <input type="checkbox" name="chkGPS"> <br>
Special Requests: <br>
    <textarea style="width:200px" name="txtarRequests" rows=5></textarea> <br>
    <input type="button" name="btnsubmit" value="Book My Rental!" onclick="calculateCost()">
    </div>
    </form>
    </body>
    </html>

最佳答案

您需要一个函数来从一组 radio 中获取选中的 radio 的值。

替换

radCar = document.rentalForm.radCar.value

radCar = getRadio('radCar');

参数在哪里,在本例中为 radCar是 radio 的名称,即 <input type="radio" name="radCar"

这是函数

function getRadio(name) {
    var radios = document.getElementsByName(name);
    for (var i = 0; i < radios.length; i++) {       
        if (radios[i].checked) return radios[i].value;
    }
}

工作示例 http://jsfiddle.net/VX9Ge/

旁白:我发现 switch 比 if/elseif/else 丛林要干净得多。

switch (radCar){
    case "batmobile":
        cost=100;
        break;
    case "gausshog":
        cost=85;
        break;
    case "lightcycle":
        cost=40;
        break;
    default: // none selected
        cost=0;
}

关于javascript - 单选按钮值在计算中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22432015/

相关文章:

javascript - 是否可以使用javascript将背景图像设置为用户电脑上的图像?

html - 将输入文本颜色设置为红色, "skip"类内的输入除外

css - 嵌套 Stylus Mixins 中的父选择器

javascript - knockout 将附加参数传递给订阅函数

php - Yii 添加另一个 $content 到模板

html - Schema.org itemref - 将多个 SportEvents 链接到一个地方

css - DIV float 问题

html - 将内容放在圆 Angular 框内

html - 如何在 Angular 组件styles.scss中使用媒体查询?

javascript - Smarty - 我如何添加 "show more links tab"