简体   繁体   English

根据下拉选项更改结果输入 - jQuery + HTML

[英]Change result input based on dropdown option - jQuery + HTML

I'm trying to build up a calculator using HTML and jQuery. 我正在尝试使用HTML和jQuery构建计算器。 So far I have: 到目前为止,我有:

HTML HTML

<form accept-charset="UTF-8" action="#" id="calc" method="post">
    <div class="item-1">
        <label for="item-1">Item 1</label>
        <input class="income" id="item-1" size="50" />
        <select id="select-item-1" class="select">
            <option value="daily">Daily</option>
            <option value="weekly">Weekly</option>
            <option value="monthly">Monthly</option>
        </select>
    </div>
    <div class="item-2">
        <label for="item-2">Item 2</label>
        <input class="income" id="item-2" size="50" />
        <select id="select-item-2" class="select">
            <option value="daily">Daily</option>
            <option value="weekly">Weekly</option>
            <option value="monthly">Monthly</option>
        </select>
    </div>
    <div class="item-3">
        <label for="item-3">Item 3</label>
        <input class="income" id="item-3" size="50" />
        <select id="select-item-3" class="select">
            <option value="daily">Daily</option>
            <option value="weekly">Weekly</option>
            <option value="monthly">Monthly</option>
        </select>
    </div>
    <hr />
    <div class="grand-total">
        <label for="grand-total">Total :</label>
        <input id="grand-total" size="50" disabled />
        <select id="grand-total-filter" class="select">
            <option value="daily">Daily</option>
            <option value="weekly">Weekly</option>
            <option value="monthly">Monthly</option>
        </select>
    </div>
</form>

SCRIPT 脚本

var $form = $('#calc'),
    $summands = $form.find('.income'),
    $sumDisplay = $('#grand-total');

    $form.delegate('.income', 'change', function (){
        var sum = 0;
        $summands.each(function () {
            var value = Number($(this).val());
            if (!isNaN(value)) sum += value;
    });

    $sumDisplay.val(sum);
});

$("#grand-total-filter").change(function() {
        var e = document.getElementById("grand-total-filter");
        var strUser = e.options[e.selectedIndex].value;
        if (strUser == 'monthly' ) {
            // Monthly
        } else if (strUser == 'weekly' ) {
            // Weekly
        } else {
            //  Default (Daily)
        }
    });

Here is a working jsFiddle with the above bits: http://jsfiddle.net/4Q8Gh/1/ 这是一个有效的jsFiddle以上位: http//jsfiddle.net/4Q8Gh/1/

The idea is that the end user should be able to add the values (item1, item2 and item3) with any option (daily, monthly, weekly) - individually - so item1/item2/item3 could have any option (daily, weekly, monthly). 这个想法是最终用户应该能够添加任何选项(每日,每月,每周)的值(item1,item2和item3) - 单独 - 所以item1 / item2 / item3可以有任何选项(每日,每周,每月) )。

The results should be displayed by default in "daily" value and the result input can be once again filtered by any of the options: daily, weekly, monthly. 默认情况下,结果应显示在“每日”值中,结果输入可以再次通过以下任何选项进行过滤:每日,每周,每月。

So for example we have: 例如,我们有:

  1. item1 = 10 (daily) item1 = 10(每日)
  2. item2 = 100 (weekly) item2 = 100(每周)
  3. item3 = 1000 (monthly) item3 = 1000(每月)

That gives us a TOTAL* of : 这给了我们一个总计*:

  • Daily: 10 + (100/5) + (1000/20) = 80 每日:10 +(100/5)+(1000/20)= 80
  • Weekly: (10*5) + 100 + (1000/4) = 400 每周:(10 * 5)+ 100 +(1000/4)= 400
  • Monthly: (10*20) + (100*4) + 1000 = 1600 每月:(10 * 20)+(100 * 4)+ 1000 = 1600

*based on the above example and considering that 1 week = 5 working days and 1 month = 4 weeks *基于上述示例并考虑1周= 5个工作日和1个月= 4周

At this point I'm lost and I'm not sure how can I throw these ideas into code using jQuery, so any help/guidance would be much much appreciated. 此时我迷失了,我不知道如何使用jQuery将这些想法转化为代码,所以任何帮助/指导都会非常感激。 Thanks in advance for any feedback! 提前感谢您的任何反馈!

PS If my approach is not correct for any reason I would be more than happy to hear your suggestions. PS如果我的方法因任何原因不正确,我会非常乐意听取您的建议。

Make 1 function that performs the full calculation and then delegate your form INPUT and SELECT change events to it. 创建1个执行完整计算的函数,然后将表单INPUT和SELECT更改事件委托给它。

http://jsfiddle.net/RZpnz/ http://jsfiddle.net/RZpnz/

function calculate() {
    var sum = 0;
    $("#calc").find("input.income").each(function() {
        var val = parseFloat($(this).val());
        if(!isNaN(val)) {
            var unit = $("#select-" + this.id).val();
            switch(unit) {
                case "daily": sum += val; break;
                case "weekly": sum += val / 5; break;
                case "monthly": sum += val / (5 * 4); break;
                default: break;
            }
        }
    });
    var unit = $("#grand-total-filter").val();
    switch(unit) {
        case "daily": sum *= 1; break;
        case "weekly": sum *= 5; break;
        case "monthly": sum *= (5 * 4); break;
        default: break;
    }
    $("#grand-total").val(sum);
}
$("#calc").delegate("input.income", "change", calculate);
$("#calc").delegate("select.select", "change", calculate);

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

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