我遇到这个问题,如果我在下拉列表的.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上?
答案 0 :(得分:0)
您可以移除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);