简体   繁体   English

为什么这个Javascript计算器不起作用

[英]Why isn't this Javascript calculator working

basically I have this form which has its field populated by php and mysql. 基本上我有这种形式,其领域由PHP和MySQL填充。 i am trying to get a javascript calculation I am using to work based on the zone they select and the quantity. 我正在尝试获取JavaScript计算,用于根据他们选择的区域和数量进行工作。 But for some reason it is not working. 但是由于某种原因,它不起作用。 My code is below. 我的代码如下。 Any ideas as to why? 有什么想法吗?

This is my Javascript code 这是我的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;
}

This is my form 这是我的表格

<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>

This is what was inserted into the database in the zone table 这就是在区域表中插入数据库的内容

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);

This is the SQL statement 这是SQL语句

CREATE TABLE Zone(
 Name char(12) not null,
 PriceMultiplier float not null default 1.0, 
 PRIMARY KEY (Name)
);

For starters you may want to adjust your HTML for your onClick events 对于初学者,您可能需要针对onClick事件调整HTML

<input class="mybutton" name="Quote" value="Calculate quote" onclick="getQuote();">

change to: 改成:

<button type="button" class="mybutton" name="Quote" onclick="getQuote();">get quote</button>

now make some changes to your function 现在对您的功能进行一些更改

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;
    }

The changes to the function that you should take note of are: 您应注意的功能更改包括:

document.getElementsByName("qty")[0].value;
document.getElementsByName("zone")[0].value;

notice the plural after the word Element(s) and then the [0] which designates which element with that name you would like to access the value of. 注意单词Element(s)之后的复数,然后是[0],它指定您要访问其名称的元素。

next inside of your if and elseIf statements we changed 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;
        }

to: 至:

 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;
        }

we changed all the double equals to all triple equal signs (check for exact match) and we need to explicitly check both sides of the or statment against the original variable. 我们将所有双等号更改为所有三等号(检查是否完全匹配),并且需要针对原始变量明确检查or语句的两侧。

Lastly, as Jonathan M pointed out we should but the re-assignment of the new value inside the getQuote function so it has the proper value. 最后,正如乔纳森·M(Jonathan M)所指出的,我们应该在getQuote函数中重新分配新值,以便它具有适当的值。

 else  {
            quote = quantity*15;
        }
        console.log("values: ", quantity,zoneSeat,quote);
        document.getElementById("quotation").value = quote;
    }   

Also notice I added a console.log("values: ", quantity,zoneSeat,quote); 还要注意,我添加了一个console.log(“ values:”,Quantity,zoneSeat,quote);

to the code. 代码。 This way, if you have the console open you should see that message once you click the "get quote" button. 这样,如果您打开了控制台,则在单击“获取报价”按钮后应该会看到该消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM