日期:2014-05-19  浏览次数:20398 次

一个关于javascript的问题,请求高手帮忙
我写了一个页面,基本上都是textbox控件,我想让焦点在一个textbox移到另一个textbox上时检验textbox的值不能为空。但是现在在检验过程中碰到死循环,有一本书上说有两种解决办法,其中一种这么写:

不要为所有与都写同一个时间处理函数,而是设置一个内嵌的处理程序把所有的代码放到时间处理程序的设置属性里则有一个例子:
<input   type= "text "   name= "field2 "   onBlur= "if   (this.value   ==   ' ')   {alert( 'enter   a   value ');   this.focus()} ">

但是我修改了以后还是不行(代码片断如下):


<asp:textbox   BackColor= "#EBEBEB "   BorderStyle= "inset "   style= "height:   12px;width:   160px;font-family:   Arial,   Verdana,   Tahoma,   Sans-Serif;font-size:   12px;vertical-align:   middle;margin-left:   5px;padding-top:   4px;padding-left:   2px;border:1px   solid   #7287c6; "   id= "vendor "   runat= "server "   onFocus=this.select()   onMouseOver=this.focus()/>


<asp:textbox   runat= "server "   BackColor= "#EBEBEB "   Columns= "8 "   id= "cdate "   cssclass= "tb1 "   onFocus=this.select()   onMouseOver=this.focus()   onBlur= "if   (this.value== ' ')   {alert( '不能为空 ');   this.focus()} "/>


<asp:textbox   runat= "server "   BackColor= "#EBEBEB "   Columns= "8 "   id= "lastm "   cssclass= "tb1 "   onFocus=this.select()   onMouseOver=this.focus()   onBlur= "if   (this.value== ' ')   {alert( '不能为空 ');   this.focus()} "/>

------解决方案--------------------
去掉onmouseover事件

<asp:TextBox runat= "server " BackColor= "#EBEBEB " Columns= "8 " ID= "cdate " CssClass= "tb1 "
onFocus= "this.select() " onBlur= "if (this.value== ' ') {alert( '不能为空 '); this.focus()} " />
------解决方案--------------------
也可以这样
<asp:TextBox runat= "server " BackColor= "#EBEBEB " Columns= "8 " ID= "cdate " CssClass= "tb1 "
onFocus= "this.select() " onMouseOver= "this.select() " onBlur= "if (this.value== ' ') {alert( '不能为空 '); this.select()} " />
------解决方案--------------------
试试下面的代码:
<input type=text onblur=vali(this) id = "1 ">
<input type=text id = "2 ">

<script>
function vali(obj)
{
if (obj.value == " ")
{
alert( '不能为空 ');
obj.focus();
}
else
{
document.getElementById( "2 ").focus();
}

}
document.getElementById( "1 ").focus();
</script>
------解决方案--------------------
<SCRIPT language= "javascript ">
<!--
function checkEnter()
{
if(event.keyCode==13 && event.srcElement.type!= "submit " && event.srcElement.type!= "button " && event.srcElement.type!= "textarea ") event.keyCode=9;
}
document.onkeydown = checkEnter;


function checkNumber()
{
if(!(((window.event.keyCode> =48)&&(window.event.keyCode <=57))||(window.event.keyCode==13)||(window.event.keyCode==46)||(window.event.keyCode==45)))
{