求助牛人!怎样阻止form的转向action的页面(用到Ajax)。
如果用户名或密码不正确局部提示错误,如果正确则转向index。<input>的Type类型是img。html文件:<formid="f_myForm"name="myFo...
如果用户名或密码不正确局部提示错误,如果正确则转向index。<input>的Type类型是img。
html文件:
<form id="f_myForm" name="myForm" action="#" method="post">
<div class="loginform" id="login_form">
<div class="input_bg1">
<input type="text" id="i_username" name="i_username" value="用户名" onfocus="clean(this)"/>
</div>
<div class="input_bg2" id="password">
<input id="i_password" name="i_password" value="密码" onfocus="changepw(this)" />
</div>
<div class="setting" id="d_setting" style="color:#F33;display:none">
<span id="s_setting">帐号或密码错误 </span><a class="rt" href="#" target="_blank">找回密码</a>
</div>
<div class="signin">
<input type="image" id="i_signin" onclick="checkSigninAjax()" class="signbtn" src="img/login_submit_btn.bmp"/>
</div>
</div>
</form>
js文件(ajax):
var xmlHttp;
var bFlag;
function creatXMLHttpRequest(){
if(window.ActiveXObject){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch(ex){}
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}
function httpStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200||xmlHttp.status==0){
bFlag=xmlHttp.responseText;
var d_settingNode=document.getElementById("d_setting");
var i_signinNode=document.getElementById("i_signin");
var s_settingNode=document.getElementById("s_setting");
var f_myFormNode=document.getElementById("f_myForm");
if(bFlag==1){
return true;
}else{
return false;
}
}
}
}
function checkSigninAjax(){
var f_myFormNode=document.getElementById("f_myForm");
creatXMLHttpRequest();
var username=document.getElementById("i_username").value;
var password=document.getElementById("i_password").value;
if(xmlHttp!=null){
var s_settingNode=document.getElementById("s_setting");
xmlHttp.onreadystatechange=httpStateChange;
xmlHttp.open("post","checkLogin",true);
xmlHttp.setRequestHeader("content-type","application/x-www-form-urlencoded");
xmlHttp.send("username="+username+"&password="+password);
if(bFlag){
f_myFormNode.submit();
}else{
s_settingNode.style.display="";
self.event.returnValue=false;
}
return false;
}else{
alter("您的浏览器不支持XMLHTTP");
}
return false;
} 展开
html文件:
<form id="f_myForm" name="myForm" action="#" method="post">
<div class="loginform" id="login_form">
<div class="input_bg1">
<input type="text" id="i_username" name="i_username" value="用户名" onfocus="clean(this)"/>
</div>
<div class="input_bg2" id="password">
<input id="i_password" name="i_password" value="密码" onfocus="changepw(this)" />
</div>
<div class="setting" id="d_setting" style="color:#F33;display:none">
<span id="s_setting">帐号或密码错误 </span><a class="rt" href="#" target="_blank">找回密码</a>
</div>
<div class="signin">
<input type="image" id="i_signin" onclick="checkSigninAjax()" class="signbtn" src="img/login_submit_btn.bmp"/>
</div>
</div>
</form>
js文件(ajax):
var xmlHttp;
var bFlag;
function creatXMLHttpRequest(){
if(window.ActiveXObject){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch(ex){}
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}
function httpStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200||xmlHttp.status==0){
bFlag=xmlHttp.responseText;
var d_settingNode=document.getElementById("d_setting");
var i_signinNode=document.getElementById("i_signin");
var s_settingNode=document.getElementById("s_setting");
var f_myFormNode=document.getElementById("f_myForm");
if(bFlag==1){
return true;
}else{
return false;
}
}
}
}
function checkSigninAjax(){
var f_myFormNode=document.getElementById("f_myForm");
creatXMLHttpRequest();
var username=document.getElementById("i_username").value;
var password=document.getElementById("i_password").value;
if(xmlHttp!=null){
var s_settingNode=document.getElementById("s_setting");
xmlHttp.onreadystatechange=httpStateChange;
xmlHttp.open("post","checkLogin",true);
xmlHttp.setRequestHeader("content-type","application/x-www-form-urlencoded");
xmlHttp.send("username="+username+"&password="+password);
if(bFlag){
f_myFormNode.submit();
}else{
s_settingNode.style.display="";
self.event.returnValue=false;
}
return false;
}else{
alter("您的浏览器不支持XMLHTTP");
}
return false;
} 展开
1个回答
展开全部
<input type="image" id="i_signin" onclick="return checkSigninAjax()" class="signbtn" src="img/login_submit_btn.bmp"/>
或者加在(个人建议最好加在form上面,这样的话,即使是用户直接按回车也可以执行AJAX)
<form id="f_myForm" name="myForm" action="#" method="post" onsubmit="return checkSigninAjax()">
然后修改JS的如下
if(bFlag==1){
loation="正确时间转向的地址"
return false;//注意这里也要返回false
}else{
return false;
}
或者加在(个人建议最好加在form上面,这样的话,即使是用户直接按回车也可以执行AJAX)
<form id="f_myForm" name="myForm" action="#" method="post" onsubmit="return checkSigninAjax()">
然后修改JS的如下
if(bFlag==1){
loation="正确时间转向的地址"
return false;//注意这里也要返回false
}else{
return false;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询