javascript - JavaScript window.location.href 代码不起作用
问题描述
我知道这个问题以前被问过很多次,但没有一个修复对我有用。我做了一个window.location。href 之前的事情并且有效,但这个没有。我知道该功能会运行,因为我使用警报对其进行了测试。有人可以在这里看到任何问题吗?
<form>
<input type="submit" name="agree" value="agree" onclick="fagree()">
<input type="submit" name="disagree" value="Decline and go to google" onclick="fdisagree()">
</form>
<script type="text/javascript">
function fagree(){
window.location.assign("index.html")
localStorage.setItem("Terms", "true")
}
function fdisagree(){
window.location.href="https://www.google.com/"
return false;
}
</script>
</body>
解决方案
如果不需要,请不要将其放入表单中(如果这是示例中的整个表单代码)。您在函数有机会触发之前提交它。
或者:
<form>
<input type="button" name="agree" value="agree" onclick="fagree()">
<input type="button" name="disagree" value="Decline and go to google" onclick="fdisagree()">
</form>
<script type="text/javascript">
function fagree(){
window.location.assign("index.html")
localStorage.setItem("Terms", "true")
}
function fdisagree(){
window.location.href="https://www.google.com/"
return false;
}
</script>
</body>
推荐阅读
- ios - 如果我使用 Firebase/Firestore 查询返回值,如何返回 TableView 数据源值?
- javascript - Angular 404(未找到)- 无法从 Angular 应用程序中的“index.html”页面加载 Javascript 文件
- python - Discord webhook 与嵌入
- php - 在模型 yii 1.1 的其他关系上调用关系
- python - 如何将每个 for 循环中的新行添加到数组中以便在 python 中创建矩阵 (m,n)?
- c# - Azure 文件存储在删除本地副本之前上传后验证文件
- c# - 从下拉列表中选择项目,然后获取项目的 ID C#
- windows - 使用 Visual Studio、Chrome 引擎为 Web 应用程序创建安装程序?
- python - 在 pyglet 中使用 opengl 渲染没有事件循环的 3d 场景
- amazon-web-services - 如何促进从连接到 S3 的 API 网关下载 CSV 和 PDF