var yanzhen={
"IsEmpty":function(obj)
{
 if($(obj).is("#txtusername")&&$(obj).val().length==0)
 {
   $(obj).next().html("<b style='color:red'>请输入你要注册的用户名!</b>");
   return false;
   $(obj).focus();
 }
 else{
     $(obj).next().remove();
 }
},}
页面调用:JS文件 和jquery文件都引用了!
为什么我用户名为空时候不提示请输入你要注册的用户名!
帮我看看有这段代码有什么问题! thanks
<asp:TextBox ID="txtusername" runat="server" CssClass="text" size=30 onblur="yanzhen.IsEmpty(this)"></asp:TextBox>&nbsp;<span>*</span> 

解决方案 »

  1.   


    老乡,你那textbox是服务器控件,编译后id、事件还可以用吧
      

  2.   

     {
       $(obj).next().html("<b style='color:red'>请输入你要注册的用户名!</b>");
       return false;
       $(obj).focus();
     }
     $(obj).focus(); 这个放在return后面能执行吗?
      

  3.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>yanzhen.html</title>

        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="this is my page">
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
         var yanzhen={
    "IsEmpty":function(obj) {
     if($(obj).is("#txtusername")&&$(obj).val().length==0) {
       $(obj).next().html("<b style='color:red'>请输入你要注册的用户名!</b>");
       //$(obj).focus();
       return false;
     } else {
         //$(obj).next().remove();//首次验证,当验证不通过span就被删除了,后面验证就找不到span标签了
         $(obj).next().html("*");
     }
     }
    }
        </script>  </head>
      
      <body>
        <input type="text" id="txtusername" onblur="yanzhen.IsEmpty(this)"/><span>*</span>
      </body>
    </html>
      

  4.   

    <script type="text/javascript">
    function txtBlur(){
       if (document.getElementById('<%=txtusername.ClientID %>').value == ''){
           document.getElementById('<%=txtusername.ClientID %>').nextSibling.innerHTML="<b style='color:red'>请输入你要注册的用户名!</b>";
           return false;
           document.getElementById('<%=txtusername.ClientID %>').focus();
       }
       else{
           document.getElementById('<%=txtusername.ClientID %>').parentElement.removeChild(document.getElementById('<%=txtusername.ClientID %>').children(1));
       }
    }
    </script>
      

  5.   

    服务器控件的话,这么取可以吗?
    我一直都是getElementsById("<%txtname.ClientID%>")这样取的。
      

  6.   

    把txtusername换成浏览器解析后的客户端控件ID就好了
      

  7.   

    这个是可以 但加上runat="server"就不行了 那怎么取值呢
      

  8.   

                   <input name="ctl00$ContentPlaceHolder1$txtusername" type="text" id="ctl00_ContentPlaceHolder1_txtusername" class="text" size="30" onblur="yanzhen.IsEmpty(this)" />&nbsp;<span>*</span> 查看源文件 生成以上的代码!
      

  9.   


    嗽一内斯馁~
    将id换成ctl00_ContentPlaceHolder1_txtusername就ok了
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>yanzhen.html</title>
        
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="this is my page">
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            var yanzhen={
                "IsEmpty":function(obj) {
                     if($(obj).is("#ctl00_ContentPlaceHolder1_txtusername")&&$(obj).val().length==0) {//因为页面编译后的id就是ctl00_ContentPlaceHolder1_txtusername这个,其他不用改
                       $(obj).next().html("<b style='color:red'>请输入你要注册的用户名!</b>");
                       $(obj).focus();
                       return false;
                     } else {
                         //$(obj).next().remove();//首次验证,当验证不通过span就被删除了,后面验证就找不到span标签了
                         $(obj).next().html("*");
                     }
                 }
            }
        </script>  </head>
      
      <body>
        <input type="text" id="txtusername" onblur="yanzhen.IsEmpty(this)"/><span>*</span>
      </body>
    </html>
      

  10.   


    可以添加自定义的属性不
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>yanzhen.html</title>
        
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="this is my page">
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            var yanzhen={
                "IsEmpty":function(obj) {
                     if($(obj).attr("key") == "userName"&&$(obj).val().length==0) {//html是可以写一些自定义属性的
                       $(obj).next().html("<b style='color:red'>请输入你要注册的用户名!</b>");
                       $(obj).focus();
                       return false;
                     } else {
                         //$(obj).next().remove();//首次验证,当验证不通过span就被删除了,后面验证就找不到span标签了
                         $(obj).next().html("*");
                     }
                 }
            }
        </script>  </head>
      
      <body>
        <input type="text" id="txtusername" key="userName" onblur="yanzhen.IsEmpty(this)"/><span>*</span>
      </body>
    </html>
      

  11.   

     if($(obj).is("#txtusername")&&$(obj).val().length==0) {
                       $(obj).next().html("<b style='color:red'>请输入你要注册的用户名!</b>");
                       
                       return false;
    $(obj).focus();
                     } 这样写吧。
      

  12.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>yanzhen.html</title>
        
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="this is my page">
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            var yanzhen={
                "IsEmpty":function(obj) {
                     if($(obj).is("#<%=txtusername.ClientID %>")&&$(obj).val().length==0) {//页面编译后的id就是<%=txtusername.ClientID %>这个,其他不用改
                       $(obj).next().html("<b style='color:red'>请输入你要注册的用户名!</b>");
                       $(obj).focus();
                       return false;
                     } else {
                         //$(obj).next().remove();//首次验证,当验证不通过span就被删除了,后面验证就找不到span标签了
                         $(obj).next().html("*");
                     }
                 }
            }
        </script>  </head>
      
      <body>
        <input type="text" id="txtusername" onblur="yanzhen.IsEmpty(this)"/><span>*</span>
      </body>
    </html>