用TextBox1、Button1和Label1在一个非常简单的and表单中重现了我的问题。我在TextBox1上使用"validation“(客户端),所以如果它是空的,就会显示一条消息。
在后台代码(服务器端)中,设置了Label1。
我使用javascript清除Label1,但它不起作用。
<head runat="server"><title></title>
<script type="text/javascript">
function ClearLabel() { $('#Label1').val(""); }
</script>
</head>
<body><form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RFV_1" runat="server"
ErrorMessage="pls enter value" Text="*" ControlToValidate="TextBox1">
</asp:RequiredFieldValidator>
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Save"
OnClientClick="ClearLabel()"
OnClick="Button1_Click" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List" />
</form></body></html>代码隐藏:
protected void Button1_Click(object sender, EventArgs e)
{if (Page.IsValid)
{Label1.Text = ("value is : " + TextBox1.Text);}}清除Label1的javascript未触发或不工作。
我哪里错了……?
发布于 2015-09-08 19:38:38
将您的代码从$('#Label1').val("");替换为$('#Label1').text("")。Set Label Text with JQuery。
编辑:
或者,您可以使用此document.getElementById("#Label1").innerHTML='';代替$('#Label1').text("")(jquery代码)。
发布于 2015-09-09 03:25:53
下面是我如何解决这个问题的-
在后面的代码中,我添加了一个"else“语句:
protected void Button1_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Label1.Text = ("value is : " + TextBox1.Text);
}
else
{
Label1.Text = "";
}
}在我的html中,我完全删除了javascript,但我仍然需要使用一个伪函数来实现OnClientClick内容,如下所示:
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RFV_1" runat="server"
ErrorMessage="pls enter value" Text="*" ControlToValidate="TextBox1">
</asp:RequiredFieldValidator>
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Save"
OnClientClick="blablabla"
OnClick="Button1_Click" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List" />
</form>
</body>注意它调用的不存在的伪函数的名称,如果没有它,它将无法工作:
OnClientClick="blablabla"虽然我不知道它是如何做到的,但它工作得很好,而且不出所料。所以我找到了一些教程,并将在接下来的几天里了解javascrit。
谢谢大家!
加迪
https://stackoverflow.com/questions/32456445
复制相似问题