所以我有这个文本框和按钮
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="Add New Category" OnClick="btnAdd_Click" />
如果单击btnAdd,我如何编写一个非常简单的jQuery方法,它检查lblName是否为空,如果为空,则只显示文本框后面的红色*。
答案 0 :(得分:0)
由于标签呈现为span,因此您必须采用jQuery的html()函数,如下所示
$('lblName').html()
创建一个函数并在onclient单击上调用它。
<asp:Button ID="btnAdd" runat="server" Text="Add New Category"
OnClientClick="return Check();" OnClick="btnAdd_Click" />
和jQuery函数如下
function check()
{
if($("#lblName").html()=="")
{
alert("your message");
return false;
}
else
return true;
}
答案 1 :(得分:0)
我有点猜测你想要的是什么,希望我能把它弄清楚。
我建议创建一个位于文本框旁边的标签,如下所示:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><asp:Label ID="ErrorLabel" runat="server"></asp:Label>
<asp:Button ID="btnAdd" runat="server" Text="Add New Category" OnClick="btnAdd_Click" />
然后使用此处的函数检查并填写*
(如果需要):
<script type="text/javascript">
function btnAdd_Click()
{
if($("#txtName").val()=="")
{
//Textbox is empty
$("#ErrorLabel").val("*");
}
else
{
$("#ErrorLabel").val("");
}
}
</script>
还有一些简单的CSS可以为它上色:
<style type="text/css">
#ErrorLabel {color:red;}
</style>