HTML Multiselect Limit

匿名 (未验证) 提交于 2019-12-03 08:28:06

问题:

Is it possible to set limit to multipleselect?

Below is an example code where user can select more than 1 value.

But, how to limit user to select not more than 3 value. Any idea?

回答1:

You can use jQuery

  $("select").change(function () {       if($("select option:selected").length > 3) {           //your code here       }   }); 


回答2:

You would do this via javascript on the client side, and then add a check on the server side as well in case the client has disabled javascript.

Here is some basic client side code to give you an idea:

              



回答3:

Script that will disallow more than 3 elements to be selected (as opposed to just validating it at submit time).

http://jsfiddle.net/v33sszgp/

var verified = []; document.querySelector('select').onchange = function(e) {   if (this.querySelectorAll('option:checked').length  -1;     });   } } 

Note there's some additional complexity related to preventing the item from actually being selected on the select and that it rather validates a user's actions and reverts it if it's invalid.



回答4:

RedFilter has the right idea with validating by javascript. Haven't tested, but you could do something like:

var options = document.all.tags("option");
var selectedCounter = 0; for (var i=0; i


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!