How to limit options in a <select> based on another option selection

前端 未结 3 1686
盖世英雄少女心
盖世英雄少女心 2021-01-16 09:29

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

3条回答
  •  清歌不尽
    2021-01-16 10:00

    On change of your dropdown fire a onchange trigger and on the basis of values make the 2nd dropdown enable or disabled.

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

    Check the fiddle here

    EDIT

    $this.value contains the value selected from "semester_credits" dropdown, now For each child of "skipped_total_credits", I am checking if that value is less than the children value then make it disabled, i.e attr("disabled", true) else make that children enabled.

提交回复
热议问题