是否可以在多选下拉列表中设置限制


Is it possible to set limit in multiple selection dropdown list?

我在我的网站中使用了多项选择下拉列表。这是正常工作的,我们可以从该列表中选择多个选项。但我只想从该列表中选择 3 个选项。是否可以为此设置限制?

我正在使用 http://www.aleixcortadellas.com/main/2009/03/20/492/中的代码

使用jQuery和以下点击功能,它会有所帮助

$(document).ready(function() 
{
    $('#slectboxid option').click(function() 
    {
        var items = $(this).parent().val();
        if (items.length > 3) {
                       alert("You can only select 3 values at a time");
           $(this).removeAttr("selected");
        }
    });
   });

编辑:使用.prop()而不是.removeAttr().removeAttr()在最新的jQuery库中被弃用

formsubmit.php将第 3 行更改为以下行:

if (isset($options) && count($options) == 3) {

然后修改第 26 行,如下所示:

echo 'Please select exactly 3 options';