Jquery MultiSelect自动回发

时间:2012-09-21 05:31:24

标签: c# jquery

我在ASP.NET中使用jQuery multiselect下拉列表我需要获取所有选定的值,但是我在回发后只从服务器端的下拉列表中获取最后一个选定的值。

脚本:

<script type="text/javascript">
    $(document).ready(function () {
        $('.department).multiselect({
            show: ["bounce", 5], hide: ["blind", 1],
            close: function () {
                debugger;
                var values = new Array();
                $(this).multiselect("getChecked").each(function (index, item) {
                     values.push($(item).val());
                });
                $("input[id*=selectedValues]").val(values.join(","));
                document.getElementById("<%=hdnDepartment.ClientID %>").value = values;
           }
       })
      .multiselectfilter();
    });
</script>
<asp:HiddenField ID="hdnDepartment" runat="server"></asp:HiddenField>
<asp:HiddenField ID="hdnEmployee" runat="server" />
<asp:DropDownList ID="ddlDepartment" runat="server" Width="150px" CssClass="department" onselectedindexchanged="ddlDepartment_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>

1 个答案:

答案 0 :(得分:1)

尝试加入值,然后将其写入隐藏字段。

$('[id*=hdnDepartment]").val(values.join()) ;

        //OR 
document.getElementById("<%=hdnDepartment.ClientID %>").value = values.join();

一旦回发发生,你也会失去通风口。所以你需要在回发完成后重新附加它。试试这个。

<script type="text/javascript">
    $(document).ready(function () {
        PostBack();

        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(PostBack);
    });

    function PostBack(){
        $('.department).multiselect({
            show: ["bounce", 5], hide: ["blind", 1],
            close: function () {
                debugger;
                var values = new Array();
                $(this).multiselect("getChecked").each(function (index, item) {
                     values.push($(item).val());
                });
                $("input[id*=selectedValues]").val(values.join(","));
                document.getElementById("<%=hdnDepartment.ClientID %>").value = values;
           }
       })
      .multiselectfilter();
    }
</script>