Jquery plugins 选择的插件最大\u选择的\u选项

Jquery plugins 选择的插件最大\u选择的\u选项,jquery-plugins,jquery-chosen,Jquery Plugins,Jquery Chosen,我使用jquery插件,注意到max\u selected\u options不起作用: 代码如下: <!doctype html> <html lang="en"> <head> <link rel="stylesheet" href="chosen/chosen.css" /> </head> <body> <select id="assets" data-placeholder="Choose as

我使用jquery插件,注意到max\u selected\u options不起作用:

代码如下:

<!doctype html> 
<html lang="en"> 
<head>
    <link rel="stylesheet" href="chosen/chosen.css" />
</head>
<body>

<select id="assets" data-placeholder="Choose assets" class="chzn-select" multiple style="width:350px;" tabindex="4">
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d">d</option>
    <option value="e">e</option>
    <option value="f">f</option>
    <option value="g">g</option>
</select>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="chosen/chosen.jquery.js" type="text/javascript"></script>
<script type="text/javascript"> 


    $(document).ready(function(){
        $(".chzn-select").chosen();
        $(".chzn-select-deselect").chosen({allow_single_deselect:true});
        $('.chzn-select').chosen({ max_selected_options: 3 });
        $(".chzn-select").bind("liszt:maxselected", function () { alert("a"); });
        $(".chzn-select").chosen().change( function () { alert("a"); } );
    });

</script>
</body>
</html>
应限制允许的最大选择数。但它不起作用。我仍然可以选择任意数量的项目。 我注意到,如果已达到所选项目的最大数量,则应触发的事件也不会触发:

$(".chzn-select").bind("liszt:maxselected", function () { alert("a"); });
我的代码有错误吗?

还有一个问题:如何将搜索功能添加到我的列表中,比如插件页面第一个示例中出现的搜索框


谢谢

调用两次
selected()
方法,这就是出现问题的原因:

$(".chzn-select").chosen();
$('.chzn-select').chosen({ max_selected_options: 3 });
取下第一个,它就工作了。您的JS代码应该是:

$(document).ready(function(){
    $(".chzn-select-deselect").chosen({allow_single_deselect:true});
    $(".chzn-select").chosen({ max_selected_options: 3 });
    $(".chzn-select").bind("chosen:maxselected", function () { alert("max elements selected"); });
    $(".chzn-select").chosen().change( function () { alert("change"); } );
});

如果我们需要更改max_selected_选项,该怎么办?这意味着从你的角度来看,它不会起作用explanation@CharlesOkwuagwu所选的
插件似乎无法在初始化后更改设置,因此我认为唯一的选择是销毁插件并重新初始化:
$(“.chzn select”).selected(“destroy”)然后
$(“.chzn select”).selected({max\u selected\u options:5})
$(document).ready(function(){
    $(".chzn-select-deselect").chosen({allow_single_deselect:true});
    $(".chzn-select").chosen({ max_selected_options: 3 });
    $(".chzn-select").bind("chosen:maxselected", function () { alert("max elements selected"); });
    $(".chzn-select").chosen().change( function () { alert("change"); } );
});