if (document.getElementById("Text1").value.length==0)
            {
                return confirm('用户名不能为空!');
                document.getElementById("Text1").value=="<font color='red'>用户名不能为空</font>";
                return false;
            }页面初始化时,有对文本框赋值,值为“”(空),脚本判断该文本框时,为什么没有进行判断呢?

解决方案 »

  1.   

    没有 html 代码,只有神仙知道出了什么问题!另,为啥用 confirm,还加了 return?可见 lz 滴思维非常混乱!
      

  2.   

    好像是:var ss=document.getElementById("Text1").value
    if(ss.length==0)
    {}
      

  3.   

    写了个例子L@_@K
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> new document </title>
        <meta name="generator" content="editplus" />
        <meta name="author" content="[email protected]" />
        <meta name="keywords" content="" />
        <meta name="description" content="" />
    </head>
    <body>
    <form method="post" action="">
        <input type="text" id="userNameTxt" />
        <input type="submit" />    
    </form>
    <script type="text/javascript">
    <!--
        document.forms[0].onsubmit = function() {
            var userName = document.getElementById("userNameTxt");
            if (userName.value.length == 0)
            {
                alert("用户名不能为空!");
                userName.value ="用户名不能为空";
                userName.style.color = "red";
                return false;
            }
        };
    //-->
    </script>
    </body>
    </html>
      

  4.   

    去掉空格判断啊
    trim(document.getElementById("Text1").value).length
      

  5.   

    return confirm('用户名不能为空!');
    单单运行这行代码,有实现弹出文本框,所以判断document.getElementById("Text1").value.length==0这条语句判断失败
    部分HTML代码如下:
    <body>
        <form id="form1" runat="server">
        <!--startprint-->
        <table class="table" enableviewstate="true" align="center" cellspacing="0" cellpadding="0"  style="width: 800; text-align: left">
            <tr>
                <td class="td">
                    姓名
                </td>
                <td class="td">
                    <input id="Text1" type="text" runat="server" class="Text2" />
                </td>
                <td class="td">
                    英文名
                </td>
                <td class="td">
                    <input id="Text2" runat="server" class="Text2" type="text" />
                </td>
               </tr>
           
      

  6.   

    按钮:
    <input id="BtnTest" title="脚本测试" type="button" class="button" value="脚本测试" 
                                                                    onclick="if (confirm('确定测试?')){judgeUserInput();}"/>
    脚本:
                if (trim(document.getElementById("Text1").value).length==0)
                {
                    confirm('用户名不能为空!'); //没有执行判断
                    document.getElementById("Text1").value=="<font color='red'>用户名不能为空</font>";
                    return false;
                }
      

  7.   

    直接判断length是有问题的
    输入空格的话length不为0
      

  8.   

    var userName = document.getElementById("Text1");
                alert("用户名" + userName);
    弹出的内容是乱七八糟的符号,正方形三角形什么的
      

  9.   

    测试过了,一切正常!注:
    由于未提供 trim 代码,因此
    trim(document.getElementById("Text1").value).length
    改为
    document.getElementById("Text1").value.length== 是比较是否相等,而非赋值!
    document.getElementById("Text1").value=="<font color='red'>用户名不能为空</font>";
    改为
    document.getElementById("Text1").value="<font color='red'>用户名不能为空</font>";L@_@K
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> new document </title>
        <meta name="generator" content="editplus" />
        <meta name="author" content="[email protected]" />
        <meta name="keywords" content="" />
        <meta name="description" content="" />
    </head>
    <body>
      <form id="form1" runat="server">
      <!--startprint--> 
      <table class="table" enableviewstate="true" align="center" cellspacing="0" cellpadding="0" style="width: 800; text-align: left">
      <tr>
      <td class="td">
      姓名
      </td>
      <td class="td">
      <input id="Text1" type="text" runat="server" class="Text2" />
      </td>
      <td class="td">
      英文名
      </td>
      <td class="td">
      <input id="Text2" runat="server" class="Text2" type="text" />
      </td>
      </tr>
      </table>
      <input id="BtnTest" title="脚本测试" type="button" class="button" value="脚本测试"  
      onclick="if (confirm('确定测试?')){judgeUserInput();}"/>
    </form>
    <script type="text/javascript">
    <!--
    function judgeUserInput()
    {
        if (document.getElementById("Text1").value.length==0)
        {
            confirm('用户名不能为空!'); //没有执行判断
            document.getElementById("Text1").value = "<font color='red'>用户名不能为空</font>";
            return false;
        }
    }
    //-->
    </script>
    </body>
    </html>
      

  10.   

    呵呵,我刚也测试出来这个问题了!
    以下为trim的代码:
             //仅删除文本框中首尾的空格 
             function   trim(s)   { 
                var   i; 
                for   (i   =   0;   i   <   s.length   &&   s.charCodeAt(i)   ==   32;   i   ++); 
                s   =   s.substring(i,   s.length); 
                for   (i   =   s.length   -   1;   i  >=   0   &&   s.charCodeAt(i)   ==   32;   i--); 
                s   =   s.substring(0,   i   +   1); 
                return   s; 
            }
    脚本是测试通过了,不过以下内容在HTML页面输出时有问题:
    document.getElementById("Text1").value = "<font color='red'>用户名不能为空</font>";文本框显示内容为"<font color='red'>□□□□□□□□□</font>";而不是我希望得到的红色字体+非乱码文字
      

  11.   

    document.getElementById("Text1").value=="<font color='red'>用户名不能为空</font>";
    楼主有点粗心了,赋值语句用=
      

  12.   

    /^$/g.test(document.getElementById("Text1").value)
    这样判断是否为空
      

  13.   

    document.getElementById("Text1").style.color="#ff0000";
    设置字体为红色
      

  14.   

    请把页面及脚本文件另存为 UTF-8 格式,
    因为 ANSI 格式无法正确编码中文!
      

  15.   

    Web 开发常用手册DHTML 参考手册
    http://download.csdn.net/source/308913JScript 语言参考
    http://download.csdn.net/source/308916CCS 样式表中文手册
    http://download.csdn.net/source/304124
      

  16.   

    L@_@K
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> new document </title>
        <meta name="generator" content="editplus" />
        <meta name="author" content="[email protected]" />
        <meta name="keywords" content="" />
        <meta name="description" content="" />
    </head>
    <body>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> new document </title>
        <meta name="generator" content="editplus" />
        <meta name="author" content="[email protected]" />
        <meta name="keywords" content="" />
        <meta name="description" content="" />
    </head>
    <body>
      <form id="form1" runat="server">
      <!--startprint--> 
      <table class="table" enableviewstate="true" align="center" cellspacing="0" cellpadding="0" style="width: 800; text-align: left">
      <tr>
      <td class="td">
      姓名
      </td>
      <td class="td">
      <input id="Text1" type="text" runat="server" class="Text2" />
      </td>
      <td class="td">
      英文名
      </td>
      <td class="td">
      <input id="Text2" runat="server" class="Text2" type="text" />
      </td>
      </tr>
      </table>
      <input id="BtnTest" title="脚本测试" type="button" class="button" value="脚本测试"  
      onclick="if (confirm('确定测试?')){judgeUserInput();}"/>
    </form>
    <script type="text/javascript">
    <!--
    var requireUsernameAlert = "用户名不能为空!";
    var chineseUserName = document.getElementById("Text1");
    function judgeUserInput()
    {
        if (chineseUserName.value.length==0)
        {
            alert(requireUsernameAlert);
            chineseUserName.value = requireUsernameAlert;
            chineseUserName.style.color = "red";
            return false;
        }
    }
    chineseUserName.onfocus = function() {
        if (this.value == requireUsernameAlert)
        {
            this.value = "";
            this.style.color = "black";
        }
    };
    //-->
    </script>
    </body>
    </html></body>
    </html>
      

  17.   

    呵呵,一般不是这么写的啊,要是非要这么写  你改这句话:
    document.getElementById("Text1").value="<font color='red'>用户名不能为空</font>";
    两个等于号做什么???
      

  18.   

    chineseUserName.onfocus = function() {
        if (this.value == requireUsernameAlert)
        {
            this.value = "";
            this.style.color = "black";
        }
    };
    没有触发,不知道为什么
      

  19.   

    //判断页面需填内容不能为空
            function judgeUserInput()
            {
                var userName = document.getElementById("Text1");
                alert("用户名" + userName.value);
                if (trim(document.getElementById("Text1").value).length ==0)
                {
                    document.getElementById("Text1").value="用户名不能为空";
                    document.getElementById("Text1").style.color= "red";
                }
            }
            //清除文本框中的错误信息
            document.getElementById("Text1").onfocus = function() {
                alert("用户名清除"); //没有执行
                if (trim(this.value) == "用户名不能为空")
                {
                    this.value = "";
                    this.style.color = "black";
                }
            };
      

  20.   

    俺回帖代码都经过测试滴!请把全部代码复制粘贴出去,并保存为 UTF-8 格式再测!
      

  21.   

    师傅们可能都去吃饭了,我就暂且先增加文本框属性onfocus="clearError()"            if(trim(document.getElementById("Text1").value) =="用户名不能为空")
                {
                    document.getElementById("Text1").value="";
                    document.getElementById("Text1").style.color= "black";
                }
      

  22.   

    恩恩,我比较笨,不知道怎么保存脚本文件格式为UTF-8不过在页面的page_load下设置
    Encoding myEncoding = Encoding.GetEncoding("gb2312");
    Response.ContentEncoding = myEncoding;
    乱码就不复存在咯!
      

  23.   

    再请教个问题,
    页面中包含一个用户自定义控件:
    <td class="td">
                    <uc1:My97DatePicker ID="begdate" class="Text2" runat="server" Format="Date" />
                </td>
    如何判断当日期没有设置时,设置焦点在该控件上?
    document.getElementById("begdate").focus();//没有效果
      

  24.   

    你的头像很可爱啊,if(document.getElementById("begdate").value==null){
    document.getElementById("begdate").focus();
    }