3

我有一个带有 Autopostback 属性的下拉列表,我想要,如果选择Select Week选项,那么它不应该回发,否则它应该回发。下面是函数。

JAVASCRIPT

function OnWeekChange() {
        var value = $("#DropDownListWeeks option:selected").val();
        if (value == "-1")
         return false; 
        else return true;
    }

ASPX

 <asp:DropDownList ID="DropDownListWeeks" runat="server" Height="23px" Width="252px"
                    OnSelectedIndexChanged="DropDownListWeeks_SelectedIndexChanged" AutoPostBack="true" ClientIDMode="Static"
                    AppendDataBoundItems="true"  onchange="return OnWeekChange();"> 
                </asp:DropDownList>

。CS

 weeks = client.GetWeeks(true);
        foreach (Roaster_Week week in weeks)
        {
            //Add weeks in the dropdowns with formatting 
            DropDownListWeeks.Items.Add(new ListItem(string.Format("{0:MMM d, yyyy}", week.StartDate) + " - " + string.Format("{0:MMM d, yyyy}", week.EndDate),week.WeekID.ToString()));
        }
        client.Close();
        DropDownListWeeks.Items.Insert(0, new ListItem("Select Week","-1"));

在我的情况下,即使我选择了-1. 您能否为我的方案提供适当的解决方案。Jquery

4

2 回答 2

3

我遇到了类似的问题,但是我使用此代码解决了。它对我有用。

function OnWeekChange() {
            var value = $("#DropDownListWeeks option:selected").val();
            if (value == "-1")
             return false; 
            else {
             __doPostBack(value , '');
            }
         }




<asp:DropDownList ID="DropDownListWeeks" runat="server" Height="23px" Width="252px"
                        OnSelectedIndexChanged="DropDownListWeeks_SelectedIndexChanged" AutoPostBack="true" ClientIDMode="Static"
                        AppendDataBoundItems="true"  onchange="return OnWeekChange();"> 
                    </asp:DropDownList>
于 2013-02-08T06:36:42.113 回答
0

删除onchange="return OnWeekChange();"并添加下面的功能将起作用。

 $(document).ready(function () {
        $("#DropDownListWeeks").change(function () {
            var value = $("#DropDownListWeeks option:selected").val();
            alert(value);
            if (value == "-1")
                return false;
            else
                return true;
        });
    });
于 2013-02-08T05:20:49.133 回答