基本上我有这个表单,其字段由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)
);
答案 0 :(得分:2)
对于初学者,您可能需要调整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之后的复数,然后是[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;
}
我们将所有双等号更改为所有三等号(检查完全匹配),我们需要明确检查或者声明的两侧与原始变量。
最后,正如Jonathan M所指出的,我们应该在getQuote函数中重新赋值,以便它具有正确的值。
else {
quote = quantity*15;
}
console.log("values: ", quantity,zoneSeat,quote);
document.getElementById("quotation").value = quote;
}
另请注意我添加了一个 console.log(&#34;值:&#34;,数量,zoneSeat,引用);
代码。这样,如果你打开了控制台,你应该在点击&#34;得到引用&#34;后看到该消息。按钮。