通过 html 选择更改 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1445263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing URL through html select
提问by Daniel Kivatinos
What is the best way to change your URL through an html select?
通过 html 选择更改 URL 的最佳方法是什么?
<select>
<option selected="selected">Change to URL X</option>
<option>Change to URL Y</option>
</select>
What Javascript should be used?
应该使用什么 Javascript?
回答by Grant Wagner
<script type="text/javascript">
function navigateTo(sel, target, newWindow) {
var url = sel.options[sel.selectedIndex].value;
if (newWindow) {
window.open(url, target, '--- attributes here, see below ---');
} else {
window[target].location.href = url;
}
}
</script>
<select onchange="navigateTo(this, 'window', false);">
<option selected="selected" value="http://www.example.com/#X">Change to URL X</option>
<option value="http://www.example.com/#Y">Change to URL Y</option>
</select>
Some useful values of target
might be 'window'
(the current window) or 'top'
(to break out of a frameset or iframe). If you want to open a new window instead, you could use navigateTo(this, 'someWindow', true);
一些有用的值target
可能是'window'
(当前窗口)或'top'
(打破框架集或 iframe)。如果你想打开一个新窗口,你可以使用navigateTo(this, 'someWindow', true);
The value of '--- attributes ---'is set using various properties as documented here for Mozillaand here for IE. For example:
'--- attributes ---'的值是使用各种属性设置的,如此处为 Mozilla和此处为 IE 所记录。例如:
'height=300,width=400,top=100,left=100,statusbar=0,toolbar=1'
回答by Stephen
If you have jQuery you could do...
如果你有 jQuery 你可以做...
javascript:
javascript:
$('#select_url').change(function(evnt){ location.href = $(this).val(); });
$('#select_url').change(function(evnt){ location.href = $(this).val(); });
html:
html:
...
...
回答by coder62
adding for example something like that in the header
在标题中添加例如类似的东西
<script language="JavaScript" type="text/javascript">
function jumpMenu(targ,selObj,restore){
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
}
</script>
and your select box looks then like this
然后你的选择框看起来像这样
<select onchange="jumpMenu('parent',this)>
<option selected="selected">Change to URL X</option>
<option value="http://www.example.com">Change to URL Y</option>
</select>