0

我正在使用 jQuery 多选,因为我正在使用按钮单击事件,该事件进入服务器端并填充另一个下拉列表中的值。根据下拉列表中选中的选择,它会填充另一个下拉列表中的值。

问题:

例如:在第一个 jQuery 多选下拉列表中,如果我选择 3 个值并根据这三个检查值关闭下拉列表,则会在另一个 jQuery 多选下拉列表中获取详细信息,但仅检查最后一个选中的值并保留。我认为这是因为按钮单击事件发生了异步回发并且值没有保留。他们是否有任何选项可以防止按钮单击事件中的回发,但应该调用单击然后只保留值。

对于这些,我附上屏幕截图:

在此处输入图像描述

基于这三个值,它被加载到另一个下拉列表中,但它显示“1 selected”,即默认的“Select”。

在此处输入图像描述

但是第二个下拉列表我可以获得在第一个下拉列表中选中的值:

在此处输入图像描述

这是我的 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;
                    if (document.getElementById("<%=hdnDepartment.ClientID %>").value != "") {
                        **$("#<%=Button1.ClientID %>")[0].click();**

                    }
                }

            })
        .multiselectfilter();
        });

protected void Button1_Click(object sender, EventArgs e)
    {
        Populate();
    }

如果我错了,请纠正我...

4

2 回答 2

1

您可以使用 OnClientClick 并根据您的要求返回 false 或 true。当 OnClientClick 返回 true 时将发生回发,否则不会回发

于 2012-09-24T07:56:39.337 回答
0
 <script type="text/javascript">
      function pageLoad() {
           $("#<%= DepartmentsListBox.ClientID %>").multiselect({
                create: function () {
                     $(this).data("initValues", $.map($("option:selected", this), function (option) { return option.value; }).join(","));
                },
                close: function () {
                     var selectedValues = $.map($("option:selected", this), function (option) { return option.value; }).join(",");
                     if (selectedValues !== $(this).data("initValues")) {
                          __doPostBack("<%= DepartmentsListBox.UniqueID %>", "");
                     }
                }
           });

           $("#<%= EmployeesListBox.ClientID %>").multiselect();
      }
 </script>


 <asp:UpdatePanel runat="server" UpdateMode="Always">
      <ContentTemplate>
           <asp:ListBox runat="server" ID="DepartmentsListBox" DataValueField="DepartmentId"
                SelectionMode="Multiple" DataTextField="Name" Width="300px"></asp:ListBox>
           <br />
           <asp:ListBox runat="server" ID="EmployeesListBox" Width="300px" SelectionMode="Multiple" AppendDataBoundItems="true"
                DataValueField="EmployeeId" DataTextField="Name"></asp:ListBox>
           <br />
           <asp:Button runat="server" ID="SubmitButton" Text="Submit" />
      </ContentTemplate>
 </asp:UpdatePanel>

服务器代码

private static Random rnd = new Random();

private string[] selectedDepartments;
private string[] SelectedDepartments
{
    get
    {
        return selectedDepartments ?? (selectedDepartments = DepartmentsListBox.Items.OfType<ListItem>().Where(item => item.Selected).Select(item => item.Value).ToArray());
    }
}

protected void Page_Init(object sender, EventArgs e)
{
    EmployeesListBox.PreRender += new EventHandler(EmployeesListBox_PreRender);
    DepartmentsListBox.SelectedIndexChanged += new EventHandler(DepartmentsListBox_SelectedIndexChanged);
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindDepartments();
    }
}

void EmployeesListBox_PreRender(object sender, EventArgs e)
{
    if (EmployeesListBox.Items.Count > 0)
    {
        EmployeesListBox.Attributes.Remove("disabled");
    }
    else
    {
        EmployeesListBox.Attributes["disabled"] = "disabled";
    }
}

private void BindDepartments()
{
    var departments = Enumerable.Range(1, 10)
        .Select(id => new { DepartmentId = id, Name = "Department " + id.ToString() });
    DepartmentsListBox.DataSource = departments;
    DepartmentsListBox.DataBind();
}

void DepartmentsListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    var employees = Enumerable.Range(1, 10)
        .Select(id => new { EmployeeId = id, DepartmentId = rnd.Next(1, 10), Name = "Employee #" + id.ToString() })
        .Where(emp => SelectedDepartments.Contains(emp.DepartmentId.ToString())).ToList();

    for (int itemIndex = EmployeesListBox.Items.Count - 1; itemIndex >= 0; itemIndex--)
    {
        var employee = employees.FirstOrDefault(emp => emp.EmployeeId.ToString() == EmployeesListBox.Items[itemIndex].Value);
        if (employee == null)
        {
            EmployeesListBox.Items.RemoveAt(itemIndex);
        }
        else
        {
            employees.Remove(employee);
        }
    }

    EmployeesListBox.DataSource = employees;
    EmployeesListBox.DataBind();
}
于 2012-09-24T10:43:28.243 回答