javascript - 单击按钮时无法重定向到新 URL
问题描述
我的网页中有以下内容:-
<select id="customernames">
//options goes here
</select>
<button id="myButton" class="float-left submit-button" >Home</button>
<script>
document.getElementById("myButton").onclick = function ()
{
var siteurl = $("#customernames").val()
alert(siteurl);
window.location.href = siteurl;
};
</script>
我想要做的是重定向到一个新的 url(新网页),当我点击一个按钮时,该 URL 将是选择列表中的一个选项值。现在目前我能够正确获取 URL 值,但是当我单击按钮时我被重定向到当前页面,所以似乎window.location.href = siteurl;
会忽略siteurl
并只是重定向到当前页面。所以有人可以建议吗?
解决方案
click
可以使用jquery事件return false
来阻止表单提交
$(function() {
$('#myButton').click(function() {
window.location.href = $("#customernames").val();
return false;
});
});
或者正如@Kamae 提到的,您可以添加e.preventDefault();
到您当前的功能
推荐阅读
- amazon-web-services - Terraform 应用检测没有变化的变化
- angular - 如何根据条件获取正确的 ngfor 索引/行号值
- mysql - Mysql - 将变量与存储的通配符进行比较
- flutter - flutter_inappwebview 5.3.2 包在选择文件或图像时使应用程序崩溃
- mysql - 我可以合并一个运行 insert into 的 .sql 文件和一个在 2 列上创建虚拟数据的 Laravel 工厂播种器吗?
- command-line-interface - 命令行界面 - 开发
- python - 编写 JSON 文件和 Python
- sql - 我加入了两张桌子。我正在尝试按城市分组,但它没有按
- python - 在可滚动框架上使用鼠标滚轮
- python - 是否可以访问用 jinja2 循环的表单输入?