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

后端 未结 8 729
一生所求
一生所求 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:36

    This would limit the user to 3 options:

    $("select").on("click", "option", function () {
        if ( 3 <= $(this).siblings(":selected").length ) {
            $(this).removeAttr("selected");
        }
    });​​​​​​​​​​
    

    Fiddle: http://jsfiddle.net/2GrYk/

提交回复
热议问题