如何限制< select>中的选项根据另一个选项选择 [英] How to limit options in a <select> based on another option selection

查看:95
本文介绍了如何限制< select>中的选项根据另一个选项选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于其他选择来限制选项的数量.例如,在此示例中,您跳过了多少学分?"应该限制​​为等于或小于上一个问题您本学期要获得多少学分?".因此,如果我每学期只修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>

解决方案

我创建了一个快速功能来帮助您,可能有一种更整洁的方法,但是它做得很好!

元素#semester_credits的变化我获取值(学期学分数,然后循环到下一个选择框,并删除具有比所选值更高的值的那些,我使用全局var进行缓存删除的选项,以防用户改变主意,我们需要将其重新添加.

$(function () {

var savedOpts = "";
$('#semester_credits').change(function() {
    //Add all options back in;
    if( savedOpts ) {
        $('#skipped_total_credits').append(savedOpts);
        savedOpts = "";
    }

    //Return false if blank option chosen;
    if( $(this).val() === "0" )
        return false;

    var chosenCreds = parseInt($(this).val());
    $('#skipped_total_credits option').each(function() {
        var thisCred = parseInt($(this).val());
        if(thisCred > chosenCreds) { 
            //Remove
            savedOpts += $(this)[0].outerHTML;
            $(this).remove();
        }
    });
});

这里有更新的小提琴

p.s凯·哈特曼(Kai Hartmann)提出的示例也是实现这一目标的好方法.

I'm trying to limit the number of options based on another selection. For instance in this example "How many credits is the class you skipped?" should be limited to equal or less than the previous question "How many total credits are you taking this semester?". So if I'm only taking 9 credits on semester the second question of how many credits I'm skipping should be equal or less than the 9 credits for the whole semester.

Any help will be greatly appreciated.

Here is the jsfiddle: http://jsfiddle.net/k7tDP/1/

Here is the 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());

    });

});

Here is the 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>

解决方案

I've created a quick function to help you out, there may be a neater way to do this, but it does the job quite nicely!

Onchange of the element #semester_credits I grab the value (number of semesters credits and then loop over the next select box and remove the ones that have a higher value that the chosen one, I use a global var to cache the removed options in case the user changes their minds and we need to add them back in.

$(function () {

var savedOpts = "";
$('#semester_credits').change(function() {
    //Add all options back in;
    if( savedOpts ) {
        $('#skipped_total_credits').append(savedOpts);
        savedOpts = "";
    }

    //Return false if blank option chosen;
    if( $(this).val() === "0" )
        return false;

    var chosenCreds = parseInt($(this).val());
    $('#skipped_total_credits option').each(function() {
        var thisCred = parseInt($(this).val());
        if(thisCred > chosenCreds) { 
            //Remove
            savedOpts += $(this)[0].outerHTML;
            $(this).remove();
        }
    });
});

Here an updated fiddle

p.s the example Kai Hartmann suggests is also a nice way to achieve this.

这篇关于如何限制&lt; select&gt;中的选项根据另一个选项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆