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

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

    Voila! No Jquery necessary.

        var is = 0;
        for (var i = 0; i < selectCountryCode.length; i++) {
            if (selectCountryCode.options[i].selected) {
                is++;
            }
            if (is > 3) {
                selectCountryCode.options[i].selected = false;              
            }
        }
    

    Jilusan

提交回复
热议问题