How do you limit options selected in a html select box?

后端 未结 8 843
一生所求
一生所求 2020-11-27 19:07

I\'m using a select tag in a form I\'m making that allows multiple selections, but I want to make the maximum amount of selections upto 10. Is this possible using javascript

8条回答
  •  长情又很酷
    2020-11-27 19:51

    I basically merged a couple of the provided answers to make something specific to a particular ID:

    $("#mySelect").on("click", "option", function(){
        var max = 3;
        if ( max <= $(this).siblings(":selected").length ) {
            alert("Only " + max + " selections allowed.");
            $(this).removeAttr("selected");
        }
    });
    

    Rob

提交回复
热议问题