[英]Why isn't this Javascript calculator working
基本上我有这种形式,其领域由PHP和MySQL填充。 我正在尝试获取JavaScript计算,用于根据他们选择的区域和数量进行工作。 但是由于某种原因,它不起作用。 我的代码如下。 有什么想法吗?
这是我的Javascript代码
function getQuote() {
var quantity = document.getElementByName("qty").value;
var zoneSeat = document.getElementByName("zone").value;
var quote;
if (zoneSeat == "balcony") {
quote= quantity*27;
}
else if (zoneSeat == "box 1" || "box 2") {
quote= quantity*75;
}
else if (zoneSeat == "box 3" || "box 4") {
quote= quantity*60;
}
else if (zoneSeat == "front stalls") {
quote= quantity*22.50;
}
else {
quote = quantity*15;
}
}
document.getElementById("quotation").value = quote;
}
这是我的表格
<form class="formDesign" action = "process.php" method="post">
<fieldset>
<p><label for="row">Row: </label><select name="row"></p>
<?php
$strRow = "SELECT DISTINCT RowNumber FROM Seat";
$result = $con->query($strRow);
?>
<?php while ($row = $result->fetch_row()): ?>
<?php $i = $row[0]; ?>
<option><?php echo $i; ?></option>
<?php
$i++;
endwhile;?>
</select>
<p><label for="seat">Zone: </label><select name="zone"></p>
<?php
$strZone = "SELECT * FROM Zone";
$result = $con->query($strZone);
?>
<?php while ($row = $result->fetch_row()): ?>
<?php $i = $row[0];?>
<option><?php echo $i?></option>
<?php
$i++;
endwhile;?>
</select>
<p><label for="numberOfTickets">Quantity:
</label><input type="number" name="qty" min="1" max="5"></p>
<p><input class="mybutton" name="Quote" value="Calculate quote" onclick="getQuote();"></p>
<label>£:</label><input name="quotation" id="quotation" type="text" readonly="readonly" value="total cost"/>
<p><input type="submit" name= "sub"></p>
</fieldset>
</form>
<p><label for="row">Row: </label><select name="row"></p>
<?php
$strRow = "SELECT DISTINCT RowNumber FROM Seat";
$result = $con->query($strRow);
?>
<?php while ($row = $result->fetch_row()): ?>
<?php $i = $row[0]; ?>
<option><?php echo $i; ?></option>
<?php
$i++;
endwhile;?>
</select>
<p><label for="seat">Zone: </label><select name="zone"></p>
<?php
$strZone = "SELECT * FROM Zone";
$result = $con->query($strZone);
?>
<?php while ($row = $result->fetch_row()): ?>
<?php $i = $row[0];?>
<option><?php echo $i?></option>
<?php
$i++;
endwhile;?>
</select>
<p><label for="numberOfTickets">Quantity:
</label><input type="number" name="qty" min="1" max="5"></p>
<p><input class="mybutton" name="Quote" value="Calculate quote" onclick="getQuote();"></p>
<label>£:</label><input name="quotation" id="quotation" type="text" readonly="readonly" value="total cost"/>
<p><input type="submit" name= "sub"></p>
</fieldset>
这就是在区域表中插入数据库的内容
insert into Zone values ('rear stalls', 1.00);
insert into Zone values ('front stalls', 1.50);
insert into Zone values ('balcony', 1.80);
insert into Zone values ('box 1', 5.00);
insert into Zone values ('box 2', 5.00);
insert into Zone values ('box 3', 4.00);
insert into Zone values ('box 4', 4.00);
这是SQL语句
CREATE TABLE Zone(
Name char(12) not null,
PriceMultiplier float not null default 1.0,
PRIMARY KEY (Name)
);
对于初学者,您可能需要针对onClick事件调整HTML
<input class="mybutton" name="Quote" value="Calculate quote" onclick="getQuote();">
改成:
<button type="button" class="mybutton" name="Quote" onclick="getQuote();">get quote</button>
现在对您的功能进行一些更改
function getQuote() {
var quantity = document.getElementsByName("qty")[0].value;
var zoneSeat = document.getElementsByName("zone")[0].value;
var quote;
if (zoneSeat === "balcony") {
quote= quantity*27;
}
else if (zoneSeat === "box 1" || zoneSeat === "box 2") {
quote= quantity*75;
}
else if (zoneSeat === "box 3" || zoneSeat === "box 4") {
quote= quantity*60;
}
else if (zoneSeat === "front stalls") {
quote= quantity*22.50;
}
else {
quote = quantity*15;
}
console.log("values: ", quantity,zoneSeat,quote);
document.getElementById("quotation").value = quote;
}
您应注意的功能更改包括:
document.getElementsByName("qty")[0].value;
document.getElementsByName("zone")[0].value;
注意单词Element(s)之后的复数,然后是[0],它指定您要访问其名称的元素。
if和elseIf语句的下一个内部,我们进行了更改
if (zoneSeat == "balcony") {
quote= quantity*27;
}
else if (zoneSeat == "box 1" || "box 2") {
quote= quantity*75;
}
else if (zoneSeat == "box 3" || "box 4") {
quote= quantity*60;
}
else if (zoneSeat == "front stalls") {
quote= quantity*22.50;
}
至:
if (zoneSeat === "balcony") {
quote= quantity*27;
}
else if (zoneSeat === "box 1" || zoneSeat === "box 2") {
quote= quantity*75;
}
else if (zoneSeat === "box 3" || zoneSeat === "box 4") {
quote= quantity*60;
}
我们将所有双等号更改为所有三等号(检查是否完全匹配),并且需要针对原始变量明确检查or语句的两侧。
最后,正如乔纳森·M(Jonathan M)所指出的,我们应该在getQuote函数中重新分配新值,以便它具有适当的值。
else {
quote = quantity*15;
}
console.log("values: ", quantity,zoneSeat,quote);
document.getElementById("quotation").value = quote;
}
还要注意,我添加了一个console.log(“ values:”,Quantity,zoneSeat,quote);
代码。 这样,如果您打开了控制台,则在单击“获取报价”按钮后应该会看到该消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.