最开始表单部分用
<form action="" method="get" οnsubmit="return ajaxLee()">
<span>用 户 名:</span><input type="text" name="username" id="username" /><br/>
<span>密 码:</span><input type="password" name="password" id="password" /><br/>
<span>验 证 码:</span><input type="verify" name="verify" id="verify"/><br/>
<input type="submit" name="submit" value="提交"/>
<input type="reset" name="reset" value="重置" />
</form>
js部分用的
function ajaxLee(){
var password = document.getElementById('password').value;
var verify = document.getElementById('verify').value;
var urls = "http://localhost/addressBook/index.php";
$.ajax({
type: "get",
url: urls,
data: "action=login&func=verify&name="+username+"&pwd="+password, //用来传递参数pageval、uid
dataType:'text', //使用jsonp
async:false,
cache:false,
success: function (msg) {
switch(msg){
case 'right':
alert('输入信息正确!欧耶!');
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre"> </span> </span><span style="font-family: Arial, Helvetica, sans-serif;"> </span><span style="font-family: Arial, Helvetica, sans-serif;">location.href="http://www.baidu.com/";</span>
break;
case 'error':
alert('密码错误!');
break;
case 'noexist':
alert('用户名不存在!');
break;
case 'failed':
alert('查询用户名失败!');
break;
default:
alert('返回结果有误!错误信息:'+msg);
}//end switch
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("XMLHttpRequest.readyState::"+XMLHttpRequest.readyState);
}
}); //end ajax
}//end func ajaxLee
这种形式进行表单验证提交,但是在js中的location.href="http://www.baidu.com/"却不能实现跳转,其实这个跳转功能是正常的,出现问题的部分是onsubmit事件是对表单提交进行验证的事件。解决方法,将<input type="submit">改成<input type="button">。
具体如下:
html部分
<form action="" method="get" >
<span>用 户 名:</span><input type="text" name="username" id="username" /><br/>
<span>密 码:</span><input type="password" name="password" id="password" /><br/>
<span>验 证 码:</span><input type="verify" name="verify" id="verify"/><br/>
<input type="button" name="submit" value="提交"/>
<input type="reset" name="reset" value="重置" />
</form>
js部分
function ajaxLee(){
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
var verify = document.getElementById('verify').value;
var urls = "index.php";
$.ajax({
type: "get",
url: urls,
data: "action=login&func=verify&name="+username+"&pwd="+password, //用来传递参数pageval、uid
dataType:'text',
async:false,
cache:false,
success: function (msg) {
switch(msg){
case 'right':
alert('输入信息正确!欧耶!');
window.location.href='http://www.baidu.com/';
break;
case 'error':
alert('密码错误!');
break;
case 'noexist':
alert('用户名不存在!');
break;
case 'failed':
alert('查询用户名失败!');
break;
default:
alert('返回结果有误!错误信息:'+msg);
}//end switch
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("ajax error!");
}
}); //end ajax
}//end func ajaxLee
改成submit之后可以进行正常跳转了,但是却不能实现回车提交功能了,然后添加回车事件即可:
html部分
<form action="" method="get" >
<span>用 户 名:</span><input type="text" name="username" id="username" /><br/>
<span>密 码:</span><input type="password" name="password" id="password" /><br/>
<span>验 证 码:</span><input type="verify" name="verify" id="verify" οnkeydοwn="javascript:butOnClick();"/><br/>
<input type="button" name="submit" value="提交" id="bsubmit" οnclick="ajaxLee()"/>
<input type="reset" name="reset" value="重置" />
</form>
js部分
function ajaxLee(){
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
var verify = document.getElementById('verify').value;
var urls = "index.php";
$.ajax({
type: "get",
url: urls,
//data:{mod:"db",name:"leedaning30",pwd:"123456",email:"2687986030",sta:"register"},
data: "action=login&func=verify&name="+username+"&pwd="+password, //用来传递参数pageval、uid
dataType:'text',
async:false,
cache:false,
success: function (msg) {
switch(msg){
case 'right':
alert('输入信息正确!欧耶!');
window.location.href='http://www.baidu.com/';
break;
case 'error':
alert('密码错误!');
break;
case 'noexist':
alert('用户名不存在!');
break;
case 'failed':
alert('查询用户名失败!');
break;
default:
alert('返回结果有误!错误信息:'+msg);
}//end switch
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("ajax error!");
}
}); //end ajax
}//end func ajaxLee
function butOnClick() {
if (event.keyCode == 13) {
var button = document.getElementById("bsubmit"); //bsubmit 为botton按钮的id
button.click();
return false;
}//end if()
}//end func butOnClick()
//doucment.form.action="http://www.baidu.com/";