<script type="text/javascript"> function yzmUpdate(){ document.yzm.src="yzm.php?"+Math.random(); } function createXML(){ var xml=false; if(window.ActiveXObject){ try{xml=new ActiveXObject("Msxml2.xmlhttp")} catch(e){ try{xml=new ActiveXObject("Microsoft.xmlhttp")} catch(e){xml=false;} } } else if(window.XMLHttpRequest){ xml=new XMLHttpRequest(); } return xml; } function check(){ var User_name_text=document.getElementById('User_name'); var User_name=User_name_text.value; var xml=createXML(); var postStr="User_name="+User_name; xml.open('POST','check.php',true); xml.setRequestHeader("content-type","application/x-www-form-urlencoded;charset=utf8"); xml.send(postStr); xml.onreadystatechange=function(){ if(xml.readyState==4 && xml.status==200){ var res=xml.responseText; if(res!=""){ alert(res); } } } } </script>输入账号离开User_name文本框时,使用Ajax对账号进行查重操作,若账号重复,则弹出消息框显示提示信息(如下图所示)。续写代码
时间: 2024-03-29 13:37:08 浏览: 57
JavaScript使用Math.random()生成简单的验证码
<html>
<head>
<title>账号查重</title>
<script type="text/javascript">
function yzmUpdate() {
document.yzm.src = "yzm.php?" + Math.random();
}
function createXML() {
var xml = false;
if (window.ActiveXObject) {
try {
xml = new ActiveXObject("Msxml2.xmlhttp")
} catch (e) {
try {
xml = new ActiveXObject("Microsoft.xmlhttp")
} catch (e) {
xml = false;
}
}
} else if (window.XMLHttpRequest) {
xml = new XMLHttpRequest();
}
return xml;
}
function check() {
var User_name_text = document.getElementById('User_name');
var User_name = User_name_text.value;
var xml = createXML();
var postStr = "User_name=" + User_name;
xml.open('POST', 'check.php', true);
xml.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=utf8");
xml.send(postStr);
xml.onreadystatechange = function () {
if (xml.readyState == 4 && xml.status == 200) {
var res = xml.responseText;
if (res != "") {
alert(res);
User_name_text.focus();
}
}
}
}
</script>
</head>
<body>
<form>
<label>账号:</label>
<input type="text" id="User_name" onBlur="check()">
<br>
<label>验证码:</label>
<input type="text">
<img src="yzm.php" name="yzm" onClick="yzmUpdate()">
</form>
</body>
</html>
阅读全文