3

所以我有这个文本框和按钮

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="Add New Category" OnClick="btnAdd_Click" />

如何在单击 btnAdd 时编写一个非常简单的 jQuery 方法,它会检查 lblName 是否为空,如果为空则在文本框后显示一个红色 *。

4

2 回答 2

0

由于标签呈现为跨度,因此您必须采用 jQuery 的 html() 函数,如下所示

 $('lblName').html()

创建一个函数并在客户端单击时调用它。

 <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;

 }
于 2013-01-14T04:29:30.007 回答
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>
于 2013-01-14T04:42:26.397 回答