我有一些这样的代码,
<form id="abc">
<input type="text" id="txt" />
</form>
现在我想像这样重定向,
var temp = $("#txt").val();
url = "http://example.com/" + temp;
window.location.replace(url);
// or window.location(url);
jQuery 有没有办法解决这个问题?它仍然让我拥有url = http://example.com
.
正如其他答案中提到的,您不需要 jQuery 来执行此操作;您可以只使用标准属性。
不过,你似乎不知道这两者之间的区别window.location.replace(url)
and window.location = url
.
-
window.location.replace(url)
将地址栏中的当前位置替换为新位置。调用该函数的页面不会包含在浏览器历史记录中。因此,在新位置上,单击浏览器中的后退按钮将使您返回到访问包含重定向 JavaScript 的文档之前正在查看的页面。
-
window.location = url
重定向到新位置。在这个新页面上,浏览器中的后退按钮将指向包含重定向 JavaScript 的原始页面。
当然,两者都有各自的用例,但在我看来,在这种情况下你应该坚持后者。
P.S.:你可能忘记了两个斜杠http:
JavaScript 第 2 行:
url = "http://abc.example/" + temp;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)