<input type='text' name='keywords' id='keywords' class='txt' value="123" onblur="w(this.id,this.value);"/>
function w(i,v){
var s = $.inArray(v,keyarr);
if(s !=-1){
alert('不能重复添加标签');
$("#"+i).focus();
return;
}
}
有一组相同name的input输入框,只简化写了一个,id都是不同的,我想实现的功能是当填写完毕之后,失去焦点,然后判断这个值知否合法,不合法的话重新聚焦。为什么我这个函数聚焦不了?onblur事件里不能再有focus吗?
------解决方案--------------------
$("#"+i).focus(); 改成
setTimeout( function(){
$("#"+i).focus();
},50);
------解决方案--------------------
<!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" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<title>无标题文档</title>
<script type="text/javascript">
function checkUsername(ele){
if(document.getElementById("username").value==""){
document.getElementById("username").setAttribute('placeholder',"不能为空");
//fix mozilla element focus bug
setTimeout(function(){
ele.focus();
});
}
}
function checkPsw(){
if(document.getElementById("psw1").value != document.getElementById("psw2").value){
document.getElementById("checkpassword").innerHTML="两次输入不一致";
document.getElementById("psw2").focus();
}
}
</script>
</head>
<body>
<div>
<form name="f2">
username:<input type="text" name="username" id="username" tabindex="1" onblur="checkUsername(this)"/><br/>
password1:<input type="password" id="psw1" tabindex="2" /><br/>
password2:<input type="password" name="psw2" id="psw2" tabindex="3" onkeypress="checkPsw()"/><br/>