如何根据其他选项选择限制<select>中的选项</select>

时间:2014-01-21 15:50:33

标签: javascript jquery

我正在尝试根据其他选择限制选项数量。例如,在这个例子中“你跳过的课程有多少学分?”应该被限制为等于或小于前一个问题“你在这个学期学习了多少学分?”。因此,如果我在学期只拿9个学分,我跳过多少学分的第二个问题应该等于或小于整个学期的9个学分。

任何帮助将不胜感激。

这是jsfiddle:http://jsfiddle.net/k7tDP/1/

这是JS:

function calculateCost() {
    'use strict';
    // enter annual tuition
    var $annualTuition = parseInt($('#annual_tuition').val());
    // tuition per semester
    var semesterTuition = Math.round($annualTuition / 3);
    // total number of credits for semester
    var $semesterCredits = parseInt($('#semester_credits').val());
    // cost of a single credit
    var singleCreditCost = semesterTuition / $semesterCredits;
    // total credits for class being skipped
    var $skippedTotalCredits = parseInt($('#skipped_total_credits').val());
    // total cost for class being skipped
    var skippedTotalCreditsCost = $skippedTotalCredits * singleCreditCost;
    // number of times skipped class meets per week
    var $skippedWeekDays = parseInt($('#skipping_class_meet').val());
    // from date
    var fromDate = $('#from').datepicker('getDate');
    // to date
    var toDate = $('#to').datepicker('getDate');
    // calculate number of weeks in date range (semester) using 'from / to' dates
    var skippedWeeks = Math.ceil((toDate - fromDate) / (1000 * 7 * 60 * 60 * 24));
    console.log(skippedWeeks);
    // total number of days in semester for class being skipped
    //var $skippedTotalDays = parseInt($('#skipped_total_days').val());
    var skippedTotalDays = $skippedWeekDays * skippedWeeks;
    // (total cost of class) / (total number of class days in semester) = cost of class
    var skippedSingleClassCost = skippedTotalCreditsCost / skippedTotalDays;
    return skippedSingleClassCost.toFixed(2);

}

$(function() {
    'use strict';

    $('#from').datepicker({
        defaultDate: '+1w',
        changeMonth: true,
        numberOfMonths: 1,
        onSelect: function() {
            //toDate = $(this).datepicker('getDate');
        }
    });

    $('#to').datepicker({
        defaultDate: '+1w',
        changeMonth: true,
        numberOfMonths: 1,
        onSelect: function() {
            //fromDate = $(this).datepicker('getDate');
        }
    });

    $('#cost').on('click', function() {
        $('.costFigure').fadeIn('fast');
        $('#costTotal').html(calculateCost());

    });

});

这是html:

<form id="costForm" action="#" onsubmit="#">

                <div>
                    <label for="annual_tuition">What is your annual tuition (estimated)?</label>
                    <div class="styled_select">
                        <select name="annual_tuition" id="annual_tuition" value="tuition amount" autofocus>
                            <option value="0">&nbsp;</option>
                            <option value="5000">$5,000</option>
                            <option value="10000">$10,000</option>
                            <option value="15000">$15,000</option>
                            <option value="20000">$20,000</option>
                            <option value="25000">$25,000</option>
                            <option value="30000">$30,000</option>
                            <option value="35000">$35,000</option>
                            <option value="40000">$40,000</option>
                            <option value="45000">$45,000</option>
                            <option value="50000">$50,000</option>
                        </select>
                    </div>
                </div>

                <div>
                    <label for="semester_credits">How many total credits are you taking this semester?</label>
                    <div class="styled_select">
                        <select name="semester_credits" id="semester_credits" value="" tabindex="2">
                            <option value="0">&nbsp;</option>
                            <option value="3">3 credits</option>
                            <option value="6">6 credits</option>
                            <option value="9">9 credits</option>
                            <option value="12">12 credits</option>
                            <option value="13">13 credits</option>
                            <option value="14">14 credits</option>
                            <option value="15">15 credits</option>
                            <option value="16">16 credits</option>
                            <option value="17">17 credits</option>
                            <option value="18">18 credits</option>
                        </select>
                    </div>
                </div>

                <div>
                    <label for="skipped_total_credits">How many credits is the class you skipped?</label>
                    <div class="styled_select">
                        <select name="skipped_total_credits" id="skipped_total_credits" value="" tabindex="2">
                            <option value="0">&nbsp;</option>
                            <option value="3">3 credits</option>
                            <option value="6">6 credits</option>
                            <option value="9">9 credits</option>
                            <option value="12">12 credits</option>
                            <option value="13">13 credits</option>
                            <option value="14">14 credits</option>
                            <option value="15">15 credits</option>
                            <option value="16">16 credits</option>
                            <option value="17">17 credits</option>
                            <option value="18" disabled>18 credits</option>
                        </select>
                    </div>
                </div>

                <div>
                    <label for="skipping_class_meet">How many times a week does the class you skipped meet?</label>
                    <div class="styled_select">
                        <select name="skipping_class_meet" id="skipping_class_meet" value="" tabindex="2">
                            <option value="0">&nbsp;</option>
                            <option value="1">1 time a week</option>
                            <option value="2">2 times a week</option>
                            <option value="3">3 times a week</option>
                            <option value="4">4 times a week</option>
                            <option value="5">5 times a week</option>
                        </select>
                    </div>
                </div>



                <div class="dateRange clearfix">
                    <label>Between what months are you enrolled in this class?</label>
                    <div style="width: 48%; float: left;">
                        <label for="from">From:</label>
                        <input type="text" id="from" name="from">
                    </div>

                    <div style="width: 48%; float: right;">
                        <label for="to">To:</label>
                        <input type="text" id="to" name="to">
                    </div>
                </div>

                <div>
                    <button id="cost" type="button">Calculate</button>
                </div>

                <div class="costFigure">
                    <h1>your missed class cost you $<span id="costTotal"></span></h1>
                </div>

            </form>

3 个答案:

答案 0 :(得分:1)

change点击dropdown点击onchange触发器,并根据值生成第二个dropdown enabledisabled

$("#semester_credits").change(function () {
        var $this=this;
        $("#skipped_total_credits").children().each(function(){
            $(this).attr("disabled",parseInt($this.value) < parseInt(this.value));
        });
    });

检查小提琴here

修改

$ this.value包含从"semester_credits"下拉列表中选择的值,现在对于"skipped_total_credits"的每个子项,我正在检查该值是否小于子值,然后禁用它,即{{ 1}}否则启用子项。

答案 1 :(得分:0)

如果您希望根据以前的答案更改问题的下拉列表,则需要向每个onchange元素添加select个事件,以便更新另一个下拉列表。然后,这应该调用一个函数来删除或添加表单中的元素。

否则,您可以为Calculate按钮添加验证功能。

答案 2 :(得分:0)

我已经创建了一个快速功能来帮助你,可能有一个更简洁的方法来做到这一点,但它做得很好!

元素的更改#semester_credits我抓住了值(学期学分的数量,然后循环到下一个选择框并删除那些具有更高值的选择的那个,我使用全局{{1如果用户改变主意并且我们需要将它们添加回来,则缓存已删除的选项。

var

此处更新了fiddle

p.s Kai Hartmann建议的例子也是实现这一目标的好方法。