简体   繁体   English

当我使用select2 jquery选择'ALL'项目时,如何禁用选择框中的所有项目

[英]How to disable all items in select box when i am selecting 'ALL' item using select2 jquery

I am working in asp.net using select2 jquery. 我正在使用select2 jquery在asp.net中工作。 in my select box i have 10 items including 'ALL'. 在我的选择框中,我有10个项目,包括“全部”。 my requirement is when i selected the ALL item , rest of the items must be disabled, when i remove the ALL tag, whole item should enabled. 我的要求是,当我选择ALL项目时,其余项目必须被禁用,当我删除ALL标签时,应启用整个项目。 http://ivaynberg.github.io/select2/ my jquery is written below. http://ivaynberg.github.io/select2/我的jQuery写在下面。

 $(document).ready(function () {
    $("#select1").select2({
        placeholder: 'Find and Select Books' 
     }).on("change", function (e) {
           alert(e.val)
    });
});

pls anyone help me???? 请有人帮我吗????

html markup: html标记:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="header1" runat="server">
<title>JQuery Select2 Plug-in</title>
<script type="text/javascript"   src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript" src="D:/DOTNET/ITS_GOOGLE_CHART_TABS_ListBOX/Scripts/select2- 3.4.1/select2.js"></script>
<link href="D:/DOTNET/ITS_GOOGLE_CHART_TABS_ListBOX/Scripts/select2-3.4.1/select2.css"  rel="stylesheet"/>

 </head>
 <body>
<form runat="server">
<select id="select1" runat="server" datasourceid="ds1" datatextfield="emp_name" multiple="true" style="width:300px"/>
<asp:SqlDataSource ID="ds1" runat="server" ConnectionString="<%$ connectionstrings:constr %>" SelectCommand="select top 10 * from emp" />

  </form>
</body>
<script type="text/javascript">
$(document).ready(function () {
    $("#select1").select2({
        placeholder: 'Find and Select Books' 

     }).on("change", function (e) {

        alert(e.val)
    });

});


</script>
</html>

  C# code:
  select1.Items.Insert(0, new ListItem("ALL", "ALL"));

Rather than disabling the items on clicking All you can select all the values .The code is here : 您可以select all the values而不是单击All来禁用项目。代码在这里:

$("#e1").select2();
$("#e1").on("change", function(e) {
    var selected = e.val;
    if ($.inArray("all", selected) !== -1) {
        var id = "e1"
        var element = $("#" + id);
        var selected = [];
        element.find("option").each(function(i, e) {
            if($(e).attr("value")=="all")
                selected[selected.length] = $(e).attr("value");
        });
        element.select2("val", selected);
    }
});

and the Jsfiddle link is http://jsfiddle.net/jEADR/734/ . 并且Jsfiddle链接是http://jsfiddle.net/jEADR/734/ hope it helps ... 希望能帮助到你 ...

courtesy: @MarcusAsplund 礼貌:@MarcusAsplund

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM