如果选择了“选项然后显示所选选项”之外的任何选项,我需要将表单重定向到另一个网址。
提前谢谢
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<form method='post'>
<select id="selectbox">
<option value="index.html" selected>Home</option>
<option value="?hello=about" >Abut Us</option>
<option value="?hello=contact" >Contact Us</option>
<option value="?hello=blog">Blog</option>
</select>
</form>
<script>
$(function(){
// bind change event to select
$('#selectbox').bind('change', function () {
var url = $(this).val(); // get selected value
if (url) { // require a URL
window.location = url; // redirect
}
return false;
});
});
</script>
</body>
</html>
最佳答案
因此,假设您的 URL 具有诸如“?hello=about”、“?hello=xxx”等查询字符串,则以下内容应该有效。将此代码放在选择框绑定(bind)方法之后。请记住,我尚未测试过此代码。
var option = location.search.replace(/(^\?hello=[^&#]+).*?$/i, "$1");
$('#selectbox').val(option);
关于javascript - 根据选择选项元素将表单重定向到不同的 URL 但重定向后未选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28808504/