window.location()无法正常工作,无法打开页面

问题描述 投票:23回答:4

我有一个带有onClick的提交按钮:

<div id="messageDiv">
<form>
<textarea name="message" rows="10" cols="20"></textarea></textarea>
<br /><br />
<input type="submit" value="Send" onClick="sendmail()">
<input type="reset" value="Reset" name='reset'>
</form>
</div>

然后我有我的sendmail:

   function sendmail()
   {   
      window.location.href = "http://www.rainbowcode.net/index.php/profiles/mail?="+mailid;
      window.location('http://www.rainbowcode.net/index.php/profiles/mail?='+mailid);
      //return true;
   }

mailid是一个全局变量,它在另一个JS函数中设置,它确实包含正确的值。为什么window.location没有打开我的页面?

如果我用mailid手动打开它可以正常工作..

javascript html
4个回答
56
投票

设置位置工作正常,但随后提交表单,这将重新加载当前页面。

从方法返回false:

function sendmail() {   
  window.location.href = "http://www.rainbowcode.net/index.php/profiles/mail?="+mailid;
  return false;
}

并在事件中返回该状态以停止提交:

<input type="submit" value="Send" onclick="return sendmail()">

3
投票

如果需要打开一个新窗口,则应使用window.open()方法。 window.location指的是当前的Windows地址,只有在使用window.location.reload()时才会重新加载CURRENT窗口。


0
投票

已经有了可靠的答案,但为什么要打击系特别是如果你用jquery或onClick调用 - 可能没有内联返回是我的观点 - 所以你可以改变submit上的动作:

document.form.action = 'http://www.rainbowcode.net/index.php'

然后,如果需要,可以选择任何表单变量,否则忽略。


0
投票

尝试使用replace函数

window.location.replace('http://www.rainbowcode.net/index.php/profiles/mail?='+mailid)
© www.soinside.com 2019 - 2024. All rights reserved.