欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript 中設(shè)置window.location.href跳轉(zhuǎn)無(wú)效問(wèn)題解決辦法

 更新時(shí)間:2017年02月09日 11:50:20   投稿:lqh  
這篇文章主要介紹了javascript 中設(shè)置window.location.href跳轉(zhuǎn)無(wú)效問(wèn)題解決辦法的相關(guān)資料,需要的朋友可以參考下

javascript 中設(shè)置window.location.href跳轉(zhuǎn)無(wú)效問(wèn)題解決辦法

問(wèn)題情況

JS中設(shè)置window.location.href跳轉(zhuǎn)無(wú)效

代碼如下:

<script type="text/javascript"> 
  function checkUser() 
{  
   if(2!=1){ 
    window.location.href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ;  
   } 
} 
 </script>  
 
<div class="extra"> 
     <a class="ui blue right floated primary button" onclick="checkUser()" href="bookConfirm?userId=${account.id}&roomNum=${room.roomNum}&stime=${stime }&etime=${etime }" rel="external nofollow" rel="external nofollow" >確認(rèn)預(yù)訂</a> 
      </div> 

原因是 a標(biāo)簽的href跳轉(zhuǎn)會(huì)執(zhí)行在window.location.href設(shè)置的跳轉(zhuǎn)之前:

如果是表單form的話(huà)  也會(huì)先執(zhí)行form提交。

提交之后 就已經(jīng)不在當(dāng)前頁(yè)面了。所以 window.location.href無(wú)效。

解決方法一

在js函數(shù)中加上

window.event.returnValue=false

這個(gè)屬性放到提交表單中的onclick事件中在這次點(diǎn)擊事件不會(huì)提交表單,如果放到超鏈接中則在這次點(diǎn)擊事件不執(zhí)行超鏈接href屬性。

改成如下代碼后window.location.href成功跳轉(zhuǎn):

<script type="text/javascript"> 
  function checkUser() 
{  
   if(2!=1){ 
    window.location.href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ;  
   window.event.returnValue=false; 
   } 
} 
 </script>  
 
<div class="extra"> 
     <a class="ui blue right floated primary button" onclick="checkUser()" href="bookConfirm?userId=${account.id}&roomNum=${room.roomNum}&stime=${stime }&etime=${etime }" rel="external nofollow" rel="external nofollow" >確認(rèn)預(yù)訂</a> 
      </div> 

解決方法二

點(diǎn)擊事件中  onclick="checkUser()"  變成 onclick="return checkUser();"

并且在 checkUser中 return  false;這樣的話(huà) a標(biāo)簽的href也不會(huì)執(zhí)行。 這樣就能window.location.href順利跳轉(zhuǎn)。

代碼如下:

<script type="text/javascript"> 
  
  function checkUser() 
{  
   if(<%=flag%>!=1){ 
    window.location.href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; 
   return false; 
   } 
} 
 </script> 
 
 <div class="extra"> 
     <a class="ui blue right floated primary button" onclick="return checkUser();"  
 
href="bookConfirm?userId=${account.id}&roomNum=${room.roomNum}&stime=${stime }&etime=${etime  
 
}">確認(rèn)預(yù)訂</a> 
      </div> 

解決方法三

如果是form體提交的話(huà)還可以把summit改成button調(diào)用js提交,這樣window.location.href也會(huì)在js提交summit之前執(zhí)行成功跳轉(zhuǎn)。

如下:

function checkUser() 
{  
   if(<%=flag%>!=1){ 
    window.location.href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; 
   return false; 
   } 
 document.getElementById("form").submit(); 
} 
 
 
  <form action="addRoom" method="post"  name="from" id="form"> 
      <table align="center" border="1" class="commTable"> 
        <tr> 
          <td class="right"><span 
            style="font-weight: blod;">房號(hào):</span></td> 
          <td><input type="text" name="roomNum" size="25" 
            id="roomNum" /></td> 
        </tr> 
        <tr> 
          <td colspan="2" align="center"><button  value="添加" 
            onclick="checkUser()" /></td> 
        </tr> 
      </table> 
    </form> 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論