程序描述:开始的时候,所有的输入框中都没有数据。点击一个输入框,不进行输入,会弹出一个“IP地址字段不能为空”的对话框,点击确定。然后,点击另一个输入框。之后,会不停的出现“IP地址字段不能为空”的对话框。测试后发现,焦点在两个输入框之间不停来回切换,形成了一个死循环。请教如何解决?
aspx中的内容:
<head runat="server">
    <title>无标题页</title>
    <script language="javascript" type="text/javascript">
    
    function checkNum(TextBox)
    {
      var ctrlValue = document.getElementById(TextBox.id).value;
      var ctrlLen = ctrlValue.length;
      if(ctrlLen==0)
      {
        alert("IP地址字段不能为空");
        TextBox.focus();
        TextBox.style.backgroundColor = "#FF0000"; //设置输入框的背景色为红色
        TextBox.select();
        return;
      }
      else
      { 
        // 判断是否为数字。isNaN()的使用:参数为字母如'a'时,返回true。参数为数字如10时,返回false。       
        if(isNaN(ctrlValue))
        {
            alert("输入的必须是数字");    
            //焦点回到这个textbox
            TextBox.focus();
            TextBox.style.backgroundColor = "#FF0000"; //设置输入框的背景色为红色
            TextBox.select();
            return;
        }
        else
        {
            if(ctrlValue%1!=0)// 判断是否为浮点数
            {
                alert("输入的数据必须是整数");
                //焦点回到这个textbox
                TextBox.focus();
                TextBox.style.backgroundColor = "#FF0000"; //设置输入框的背景色为红色
                TextBox.select();
                return;
            }
            if(ctrlValue>255||ctrlValue<0)
            {
                alert("输入的数据必须在0至255之间");
                //焦点回到这个textbox
                TextBox.focus();
                TextBox.style.backgroundColor = "#FF0000"; //设置输入框的背景色为红色
                TextBox.select();
                return;
            }              
        }     
    }
       TextBox.style.backgroundColor = "#FFFFFF"; //设置输入框的背景色为红色
       return;
    }
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server" Height="81px" Style="left: 23px; position: absolute; top: 80px" Width="498px">
            起始地址 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
            终止地址<br />
            <input id="Text1" style="width: 50px" type="text" onblur="checkNum(this)"/>
            <input id="Text2" style="width: 50px" type="text" onblur="checkNum(this)"/>
            <input id="Text3" style="width: 50px" type="text" onblur="checkNum(this)"/>
            <input id="Text4" style="width: 50px" type="text" onblur="checkNum(this)"/>
            &nbsp; &nbsp;&nbsp;
            <input id="Text5" style="width: 50px" type="text" onblur="checkNum(this)"/>
            <input id="Text6" style="width: 50px" type="text" onblur="checkNum(this)"/>
            <input id="Text7" style="width: 50px" type="text" onblur="checkNum(this)"/>
            <input id="Text8" style="width: 50px" type="text" onblur="checkNum(this)"/><br />
            地址数量<br />
            <input id="Text9" style="width: 50px" type="text" onblur="checkNum(this)" />
         </asp:Panel>
    </div>
    </form>
</body>
</html>.cs文件中的内容:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;namespace _99
{
    public partial class test1011 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {        }
    }
}

解决方案 »

  1.   

    把onblur改为统一验证不行么???
      

  2.   

    这个可以。。用onsubmit="checkNum()" 来触发函数。这样一来你获取文本框数据的代码得改了。
      

  3.   

    var ctrlValue = document.getElementById(TextBox.id).value;
    你传了this进来,就不需要document.getElementById(TextBox.id)再获取一次了。。
    直接var ctrlValue = TextBox.value;就正常了
      

  4.   

    噢,明白你意思了。输入框不停失去焦点,肯定会进入死循环。。因为你用了onblur
      

  5.   

    你要给input加上onfocus事件,和一个锁~ <input id="Text1" style="width: 50px" type="text" onblur="checkNum.blur(this)" onfocus="checkNum.focus(this)"/>
                <input id="Text2" style="width: 50px" type="text" onblur="checkNum.blur(this)" onfocus="checkNum.focus(this)"/>
                <input id="Text3" style="width: 50px" type="text" onblur="checkNum.blur(this)" onfocus="checkNum.focus(this)"/>
                <input id="Text4" style="width: 50px" type="text" onblur="checkNum.blur(this)" onfocus="checkNum.focus(this)"/>
                &nbsp; &nbsp;&nbsp;
                <input id="Text5" style="width: 50px" type="text" onblur="checkNum.blur(this)" onfocus="checkNum.focus(this)"/>
                <input id="Text6" style="width: 50px" type="text" onblur="checkNum.blur(this)" onfocus="checkNum.focus(this)"/>
                <input id="Text7" style="width: 50px" type="text" onblur="checkNum.blur(this)" onfocus="checkNum.focus(this)"/>
                <input id="Text8" style="width: 50px" type="text" onblur="checkNum.blur(this)" onfocus="checkNum.focus(this)"/><br />
                地址数量<br />
                <input id="Text9" style="width: 50px" type="text" onblur="checkNum.blur(this)" onfocus="checkNum.focus(this)"/>
     var checkNum = function () {
            //验证锁
            var lock = false,
             el = {};
         return {
    blur: function(TextBox){
         //var ctrlValue = document.getElementById(TextBox.id).value;
         if (!lock){        
             el = TextBox;
         var ctrlValue = TextBox.value;
                   var ctrlLen = ctrlValue.length;       
                   if(ctrlLen==0){
                     alert("IP地址字段不能为空");           
             TextBox.focus();
                 TextBox.style.backgroundColor = "#FF0000"; //设置输入框的背景色为红色
                 TextBox.select();
               lock = true;
                     return;
                   } else { 
                     // 判断是否为数字。isNaN()的使用:参数为字母如'a'时,返回true。参数为数字如10时,返回false。       
                     if(isNaN(ctrlValue)) {
                         alert("输入的必须是数字");    
                         //焦点回到这个textbox          
             TextBox.focus();
                         TextBox.style.backgroundColor = "#FF0000"; //设置输入框的背景色为红色
                         TextBox.select(); 
                      lock = true;                    
                         return;
                     } else {
                     // 判断是否为浮点数
                         if(ctrlValue%1!=0){
                             alert("输入的数据必须是整数");
                             //焦点回到这个textbox
                             TextBox.focus();
                             TextBox.style.backgroundColor = "#FF0000"; //设置输入框的背景色为红色
                             TextBox.select();
                          lock = true;
                             return;
                         }
                         if(ctrlValue>255||ctrlValue<0) {
                             alert("输入的数据必须在0至255之间");
                             //焦点回到这个textbox
                             TextBox.focus();
                             TextBox.style.backgroundColor = "#FF0000"; //设置输入框的背景色为红色
                             TextBox.select();
                          lock = true;
                             return;
                         }              
                     }     
                 }
                 TextBox.style.backgroundColor = "#FFFFFF"; //设置输入框的背景色为红色
                 return;
             }
            
    },
    focus: function(){
                                    //如果验证没通过就点击其他input,则将焦点返回该input
    if (lock){
    el.focus(); 
    }
    }
            }
     
        }()
      

  6.   

    楼主可否不用弹出框呢,直接把报错信息写在文本框的右边,或者创建一个层浮在文本框上。如果是弹出框,那么你点击确定然后点另一个文本框,相当于又触发onblur事件,所以弹出框不是好的解决方案。
      

  7.   


    其实不用弹出框会比较好点,但是如果楼主非要用也没办法,其实弹出框也有个办法就是将其他input都disabled掉。。当验证通过了才允许点击下一个input
      

  8.   

    10楼的代码中JScript code最后多了一个“}”。
    另外,程序似乎还是有问题,在浏览器中查看该网页的时候,有错误“checkNum为空或不是对象”。
      

  9.   


    你将代码放到body下面了么?
    这个要放倒上面去
      

  10.   

    当输入内容不合法走else语句时把焦点重新给第一个输入框就行了 
      

  11.   

    放在body上面了。这种写法我第一次接触,不知道为什么报出那个错误。希望你能再帮我看看你写的这个代码,我很想知道为什么会报出那个错误。
    另外,你的写法给了提示,我用另一种方法实现了加锁的功能,不过还是写了两个函数。一个函数在控件获得焦点时调用,一个函数在控件失去焦点时调用。与原来的相比,我加了两个隐藏的html控件input(text),代表要加的锁。一个控件的value用来存储获得焦点的控件的ID,一个控件的value用来记录false或者true,表示获得了焦点的控件的数据是否通过了验证。
    另外,按照朋友们的提法,用一个隐藏的input(text)控件来输出错误信息。(我还不会“创建一个层浮在文本框上”,所以只好暂时这样凑合了)
    谢谢各位朋友了!