根据select option元素将表单重定向到不同的URL,但重定向后未选中
Redirect form to different URL based on select option element But not selected after redirect
如果选择了Option之外的任何选项,则我需要有重定向到另一个url的表单。然后显示所选的Option。
提前感谢
<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"等查询字符串,则以下内容应该有效。在selectbox绑定方法之后删除此代码。请记住,我还没有测试过这个代码。
var option = location.search.replace(/(^'?hello=[^&#]+).*?$/i, "$1");
$('#selectbox').val(option);
只需使用
if (url) { // require a URL
window.location.href = url; // redirect
}
因为在选项值中,您没有传递完整的URL。
试试这个:
$(function(){
// bind change event to select
$('#selectbox').bind('change', function () {
var url = $(this).val(); // get selected value
if (url) { // require a URL
$( 'form' ).attr( 'action', url ).submit();
}
return false;
});
});
我假设您希望表单做的比问题中指示的更多,否则这只是一个基本的跳转导航。如果你确实有更多的数据要传输,那么window.location不会从表单中发布这些数据。你需要做以下操作:
$(function(){
// bind change event to select
$('#selectbox').bind('change', function () {
var url = $(this).val(); // get selected value
if (url) {
// set the 'action' attribute to the
// value of the url var and submit the form.
$('form').attr('action', url).submit();
}
return false;
});
});
相关文章:
- safari扩展中的重定向url
- 将重定向URL链接添加到JS函数
- 使用phonegap重定向URL
- 在php中htaccess重定向url后,无法加载css和js
- 如何在重定向 URL 的标头中拦截和获取位置
- 如何使用javascript查询重定向url
- 如何使用javascript获取重定向url
- 重定向(URl)提供控制器视图 URI模式
- 带有Authorization标头的JavaScript重定向URL
- 从数组随机重定向URL
- 在重定向URL上使用HubSpot表单数据
- 如何在 Javascript 或 jQuery 中重定向 URL
- 快速发送文件和重定向 URL
- 使用javascript基于登录用户重定向URL链接
- 如何使用javascript完全重定向URL
- JavaScript var 值,重定向 URL 为空
- 如何在WordPress中即时重定向URL
- 在选项卡更改时重定向 URL
- 已更新:从另一个页面重定向 url 传递参数
- 从页面文档文本重定向 URL