例子:一个注册页面用用户名和密码两个输入框。
需求:如果用户名为空,就调用focus()函数,将光标重新定位到用户名输入框;如果密码输入框为空,就将光标重新点位到密码输入框问题:如果当用户将光标切人用户名输入框,但是并没有填写就将光标切入密码输入框,这个时候光标就会上下来回的切换,知道卡死为止!请问如何做到这个效果。。<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<table>
<tr>
<td>Name:</td>
<td><input type="text" id="txtUserName" onblur="OnCheckName()" /></td>
</tr>
<tr>
<td>Pwd:</td>
<td><input type="text" id="txtPwd" onblur="OnCheckPwd()" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" id="btnReg" onclick="OnSub()" value="注册" /></td>
</tr>
</table>
</body>
</html>
<script type="text/javascript">
var nameObj = document.getElementById("txtUserName");
var pwdObj = document.getElementById("txtPwd");
function OnCheckName(){
if(nameObj.value == ""){
nameObj.focus();
return false;
}
}
function OnCheckPwd(){
if(pwdObj.value == ""){
pwdObj.focus();
return false;
}
}
function OnSub(){
if(OnCheckName && OnCheckPwd){
return true;
}else{
return false;
}
}
</script>
需求:如果用户名为空,就调用focus()函数,将光标重新定位到用户名输入框;如果密码输入框为空,就将光标重新点位到密码输入框问题:如果当用户将光标切人用户名输入框,但是并没有填写就将光标切入密码输入框,这个时候光标就会上下来回的切换,知道卡死为止!请问如何做到这个效果。。<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<table>
<tr>
<td>Name:</td>
<td><input type="text" id="txtUserName" onblur="OnCheckName()" /></td>
</tr>
<tr>
<td>Pwd:</td>
<td><input type="text" id="txtPwd" onblur="OnCheckPwd()" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" id="btnReg" onclick="OnSub()" value="注册" /></td>
</tr>
</table>
</body>
</html>
<script type="text/javascript">
var nameObj = document.getElementById("txtUserName");
var pwdObj = document.getElementById("txtPwd");
function OnCheckName(){
if(nameObj.value == ""){
nameObj.focus();
return false;
}
}
function OnCheckPwd(){
if(pwdObj.value == ""){
pwdObj.focus();
return false;
}
}
function OnSub(){
if(OnCheckName && OnCheckPwd){
return true;
}else{
return false;
}
}
</script>
解决方案 »
- websocket 数据解析
- 如何删除JSP页面表格中的一行数据
- 帮忙修改代码,把输出“80天8小时8分8秒”改成“80天08小时08分08秒”
- 问高手一个用Js控制iframe的问题
- js的 window.load 和window.onload 有什么区别?
- 对input来说,使用select函数可以让该输入框中的文字被选中,那使用什么函数可以使这些文字不被选中?
- 怎么屏蔽鼠标右键
- 关于关闭窗口的
- 这种效果有什么工具能做出来呀?或者源代码???wait thanks jjjjjj
- 请问这个页面的导航栏隐藏是如何实现的.急用!
- 我做了个网站,有首页flash的,首页是index.html,我的flash上,有一个按钮 进入网站,指向 www.xxx.com/index.php
- 如何制作pki证书
function OnSub(){
if(OnCheckName && OnCheckPwd){
return true;
}else{
while(true){
nameObj.focus();
pwdObj.focus(); }
}
}
要卡死?
对啊,你这样是在提交的时候验证,这样写估计不会吧!!!但是每个输入框的onblur()时间里面也需要判断啊。。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>无标题文档</title></head><body>
<table>
<tr>
<td>Name:</td> //最好加上name吧
<td><input type="text" id="txtUserName" name="txtUserName" onblur="OnCheckName()" /></td>
</tr>
<tr>
<td>Pwd:</td>
<td><input type="text" id="txtPwd" name="txtPwd" onblur="OnCheckPwd()" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" id="btnReg" onclick="OnSub()" value="注册" /></td>
</tr>
</table>
<script type="text/javascript">
var nameObj = document.getElementById("txtUserName");
var pwdObj = document.getElementById("txtPwd");
function OnCheckName(){
if(nameObj.value == ""){
nameObj.focus();
return false;
}
//只有返回false;没有true,提交时不继续往下执行
return true;
}
function OnCheckPwd(){
if(pwdObj.value == ""){
pwdObj.focus();
return false;
}
//只有返回false;没有true,提交时不继续往下执行
return true;
}
function OnSub(){
// if(OnCheckName() && OnCheckPwd())这里要加括号
if(OnCheckName() && OnCheckPwd()){
return true;
}else{
return false;
}
}
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>无标题文档</title></head><body>
<table>
<tr>
<td>Name:</td> //最好加上name吧
<td><input type="text" id="txtUserName" name="txtUserName" onblur="OnCheckName()" /></td>
</tr>
<tr>
<td>Pwd:</td>
<td><input type="text" id="txtPwd" name="txtPwd" onblur="OnCheckPwd()" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" id="btnReg" onclick="OnSub()" value="注册" /></td>
</tr>
</table>
<script type="text/javascript">
var nameObj = document.getElementById("txtUserName");
var pwdObj = document.getElementById("txtPwd");
function OnCheckName(){
if(nameObj.value == ""){
nameObj.focus();
return false;
}
//只有返回false;没有true,提交时不继续往下执行
return true;
}
function OnCheckPwd(){
if(pwdObj.value == ""){
pwdObj.focus();
return false;
}
//只有返回false;没有true,提交时不继续往下执行
return true;
}
function OnSub(){
// if(OnCheckName() && OnCheckPwd())这里要加括号
if(OnCheckName() && OnCheckPwd()){
return true;
}else{
return false;
}
}
</script>
</body>
</html>
function Check(){
if(nameObj.value == ""){
nameObj.focus();
return false;
}
else if(pwdObj.value == ""){
pwdObj.focus();
return false;
}
return true;
}
我暂时没考虑你的那个光标,
再试试
在check方法里面加document.form[0].submit();这样提交会好点,
避免出现那样子的问题、
我不是要用submit。我是需要在输入框的onblur事件后面加那个方法。。为什么在被调用的方法没用用呢??
如果考虑其它的话,
在<input type="submit" onclick="***; return false">也加上这个吧,
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>无标题文档</title>
</head><body>
<table>
<tr>
<td>Name:</td> //最好加上name吧
<td><input type="text" id="txtUserName" name="txtUserName" onblur="return OnCheck();" /></td>
</tr>
<tr>
<td>Pwd:</td>
<td><input type="text" id="txtPwd" name="txtPwd" onblur="return OnCheck();" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" id="btnReg" onclick="return OnCheck();" value="注册" /></td>
</tr>
</table>
<script type="text/javascript">
var nameObj = document.getElementById("txtUserName");
var pwdObj = document.getElementById("txtPwd");
function OnCheck() {
if (nameObj.value == "") {
nameObj.focus();
return false;
}
else if (pwdObj.value == "") {
pwdObj.focus();
return false;
}
else {
alert(nameObj.value + pwdObj.value);
}
}
</script>
</body>
</html>我试过了,完全可以!!
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>无标题文档</title>
</head><body>
<form id="form1" name="form1" action="reg.asp" method="post" onsubmit="javascript:return checkSubmit();">
<table>
<tr>
<td>Name:</td>
<td><input type="text" id="txtUserName" name="txtUserName" onblur="return OnCheck();" /></td>
</tr>
<tr>
<td>Pwd:</td>
<td><input type="text" id="txtPwd" name="txtPwd" onblur="return OnCheck();" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" id="btnReg" value="注册" /></td>
</tr>
</table>
</form><script type="text/javascript">
var nameObj = document.getElementById("txtUserName");
var pwdObj = document.getElementById("txtPwd");
function checkSubmit() {
if(nameObj.value.replace(/ /g,"") == "") {
alert("姓名不能留空!");
nameObj.focus();
return false;
}
if(pwdObj.value.replace(/ /g,"") == "") {
alert("密码不能留空!");
pwdObj.focus();
return false;
}
alert(nameObj.value + pwdObj.value);
return true;
}
</script>
</body>
</html>表单要用form标签括起来,最好是用form来提交。如果要用ajax提交,checkSubmit()里总是返回false,就可以防止表单提交导致页面刷新了。
if(用户名==null)用户名.focus;
else if(密码==null)密码.focus;
}
试试这样写,肯定没问题了。