程序描述:开始的时候,所有的输入框中都没有数据。点击一个输入框,不进行输入,会弹出一个“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">
起始地址
终止地址<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)"/>
<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)
{ }
}
}
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">
起始地址
终止地址<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)"/>
<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)
{ }
}
}
解决方案 »
- 3d旋转标签问题
- jQuery:在页面加载完毕之后,怎么在head里加title、meta
- 急需帮忙~关于js
- 【百度分享】js selector设计及实现——实现思路(3)
- 有关两个div间鼠标移动的问题?
- 机顶盒不支持js怎么办?请高手解决!
- 求个正则,将不处于html和js标记内的关键字这个字符串替换
- 网站用户自动登录一般都是保存COOKIE到客户端,请问是保存用户名和密码还是光保存用户名就行?
- 请教:event.srcElement的问题
- 如何让浏览器端打开我的网页时自动下载并运行一个可执行程序。
- DIV拖动的代码(希望能对新人有点帮助)
- 点击一个按钮 弹出一个可以让用户选择路径的对话框,并把路径保存到一个文本框中
你传了this进来,就不需要document.getElementById(TextBox.id)再获取一次了。。
直接var ctrlValue = TextBox.value;就正常了
<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)"/>
<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();
}
}
}
}()
其实不用弹出框会比较好点,但是如果楼主非要用也没办法,其实弹出框也有个办法就是将其他input都disabled掉。。当验证通过了才允许点击下一个input
另外,程序似乎还是有问题,在浏览器中查看该网页的时候,有错误“checkNum为空或不是对象”。
你将代码放到body下面了么?
这个要放倒上面去
!
另外,你的写法给了提示,我用另一种方法实现了加锁的功能,不过还是写了两个函数。一个函数在控件获得焦点时调用,一个函数在控件失去焦点时调用。与原来的相比,我加了两个隐藏的html控件input(text),代表要加的锁。一个控件的value用来存储获得焦点的控件的ID,一个控件的value用来记录false或者true,表示获得了焦点的控件的数据是否通过了验证。
另外,按照朋友们的提法,用一个隐藏的input(text)控件来输出错误信息。(我还不会“创建一个层浮在文本框上”,所以只好暂时这样凑合了)
谢谢各位朋友了!