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

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

    Using jQuery you can attach a function to the change event and since it is a multiple select the val() will be an array. You can always check the length of the array and do something if its a predefined size. The following code demonstrates how you will know how many items are selected.

      $("#select").change(function(){
            alert($(this).val().length);
      });   
    

提交回复
热议问题