【问题标题】:Drop Down List Selected Value Gets Reset On PostBack When Getting Disabled With JQuery使用 JQuery 禁用时,下拉列表选定值在回发时重置
【发布时间】:2014-04-24 12:53:10
【问题描述】:

我有这个问题,如果我在下拉列表的 .change 事件上禁用 JQuery 控件,该控件将重置为其默认状态。

我有以下代码:

ASP.NET:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

</head>

<script type='text/javascript' src="jquery-1.11.0.min.js"></script>
<body>
<form id="form1" runat="server">
<div>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
    <asp:ListItem Value="1">Item1</asp:ListItem>
    <asp:ListItem Value="2">Item2</asp:ListItem>
    </asp:DropDownList>
</div>
</form>
</body>

<script type="text/javascript">
$("#DropDownList1").change(function () {

    $("#DropDownList1").attr("disabled", "disabled");

});
</script>
</html>

代码背后:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

因此,使用该代码,如果您将所选项目从 DropDownList1 切换,它将在 PostBack 上重置为其默认状态。如果控件没有被 Jquery 禁用,它不会重置控件。

如何使它在被禁用时在 PostBack 上保持其值?

【问题讨论】:

  • 你需要什么,取消回发?
  • 您可能需要将 JavaScript 的当前状态存储在 sessionstate 对象中并在回发时调用它。
  • 克里斯,我现在就试试这个。
  • 但是如果您需要进行回发,为什么不禁用 onselecteIndexchange 中的 DDL,简单且没有复杂性。
  • 回发时仍会重置该值

标签: c# javascript jquery asp.net


【解决方案1】:

您可以删除Autopostback="True",这样就不会发生回发..

<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Value="1">Item1</asp:ListItem>
    <asp:ListItem Value="2">Item2</asp:ListItem>
    </asp:DropDownList>

Autopostback="True" 在更改下拉项时必须对后面的代码执行某些操作时设置..

尝试像这样禁用..

$("#DropDownList1").attr("disabled", true);

【讨论】:

  • 感谢您的快速回复。我的要求需要这个下拉列表回发。我唯一想做的就是在回发完成之前暂时禁用下拉菜单。我只是不明白为什么下拉列表在回发之前没有被禁用时会保持其值。
  • 回发后下拉菜单是否被禁用?
  • 否,它被重置为默认状态。 (selectedindex = 0 并且启用 = true)
  • 那么您是否更喜欢禁用事件后面代码中的控件..例如 Selectedindexchanged,Regards..
  • 当我禁用 Selectedindexchanged 上的下拉菜单时,它工作正常。它没有重置所选项目..
猜你喜欢
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多