简体   繁体   English

下拉菜单计算

[英]Drop down menu calculation

How to calculate the total of the value of two drop down menus that has been selected by the user. 如何计算用户已选择的两个下拉菜单的值的总和。 I have no pre determined values in HTML. 我在HTML中没有预先确定的值。 Here is my code, i want to multiply the number of tickets by the price selected and show it to the user. 这是我的代码,我想将门票数量乘以所选价格并将其显示给用户。 thanks. 谢谢。

HTML 的HTML

    <select name="price" id="price" style="width:120px">

    <option>Select Price</option>
</select>
<br>
<br>
<select name="tickets" id="tickets" style="width:120px">
    <option>Select Tickets</option>
</select>

   <br>
   <br>
<input name="button" type="button" value="Submit" onClick="return validate2()"/>   
<input name="reset" type="reset" value="Reset" />
</form>

JavaScript 的JavaScript

switch (artist.selectedIndex)
{
    case 0:
        var venueList   = ["Select Venue"];
        var dateList    = ["Select Date"];
        var ticketList  = ["Select Tickets"];
        var priceList   = ["Select Price"];
        fillList(venue, venueList);
        fillList(date, dateList);
        fillList(ticket, ticketList);
        fillList(price, priceList);
        break;

    case 1:

        var venueList   = ["Select Venue", "London"];
        var dateList    = ["Select Date", "17th July", "18th July"];
        var ticketList  = ["Select Tickets", "1", "2", "3", "4", "5", "6"];
        var priceList   = ["Select Price", "£30", "£45", "£70"];
        fillList(venue, venueList);
        fillList(date, dateList);
        fillList(ticket, ticketList);
        fillList(price, priceList);
        break;

    case 2:

        var venueList   = ["Select Venue", "Manchester", "Glasgow"];
        var dateList    = ["Select Date"]
        var ticketList  = ["Select Tickets", "1", "2", "3", "4", "5", "6"];
        var priceList   = ["Select Price", "£35", "£50", "£60"];
        fillList(venue, venueList);
        fillList(date, dateList);
        fillList(ticket, ticketList);
        fillList(price, priceList);

        // adding onchange event on venue when the user selects rod stewart
        venue.onchange = function ()
        {
            var dateList;
            switch(venue.selectedIndex)
            {
                case 0: dateList = ["Select Date"]; break;
                case 1: dateList = ["Select Date", "18th July", "20th July"]; break;
                case 2: dateList = ["Select Date", "22nd July", "23rd July"]; break;
            }
            fillList(date, dateList);
        }


        break;

First off I'm taking the assumption that venue, date, ticket and price are references to the select elements. 首先,我假设场地,日期,门票和价格是对select元素的引用。

However with your current code I'd suggest something like this - 但是,根据您当前的代码,我建议使用类似的方法-

function validate2() {
    var truePrice = parseFloat(String.replace(price.options[price.selectedIndex].value, '£', '')),
        noOfTickets = parseInt(ticket.options[ticket.selectedIndex].value);

    return truePrice * noOfTickets;        
}

http://fiddle.jshell.net/J4Ryy/3/ Here is a working fiddle based on my answer :) http://fiddle.jshell.net/J4Ryy/3/这是根据我的回答而起作用的小提琴:)

You have to get the value of the two dropdowns in to variables. 您必须获取变量中两个下拉菜单的值。 The value of your dropdowns or other hthml tags is a string. 您的下拉菜单或其他hthml标签的值是一个字符串。

You have to convert the string into a number. 您必须将字符串转换为数字。 How you can do this you can read here: How do I convert a String into an integer 如何做到这一点,您可以在这里阅读: 如何将字符串转换为整数

After that you can do normal operations like 之后,您可以执行常规操作,例如

var c = var a + var b

Greethings 护齿

Using jQuery: 使用jQuery:

<script src="https://code.jquery.com/jquery.js"></script>
<script>
    $('select').on('change', function (e) {
        var tickets = $('#tickets').val();
        var price = $('#price').val().substring(1);
        alert(tickets * price);
    });
</script>

Explanation: everytime the select box changes we calculate the total price by getting the values of the select boxes. 说明:每次更改选择框时,我们都会通过获取选择框的值来计算总价。 The .substring(1) removes the pound symbol by taking a substring from position 1 to the end. .substring(1)通过从位置1到末尾取一个子字符串来删除井号。

You can change the alert to do something else, like put it in a box: 您可以更改警报以执行其他操作,例如将其放在框中:

$('#resultBox').html(tickets * price);

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

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