【问题标题】:Changing URL through html select通过 html 选择更改 URL
【发布时间】:2010-11-29 12:54:09
【问题描述】:

通过 html 选择更改 URL 的最佳方法是什么?

<select>
<option selected="selected">Change to URL X</option>
<option>Change to URL Y</option>
</select>

应该使用什么 Javascript?

【问题讨论】:

标签: html url url-rewriting html-select


【解决方案1】:
<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>

target 的一些有用值可能是'window'(当前窗口)或'top'(打破框架集或 iframe)。如果你想打开一个新窗口,你可以使用navigateTo(this, 'someWindow', true);

'--- attributes ---' 的值是使用记录在 here for Mozillahere for IE 中的各种属性设置的。例如:

'height=300,width=400,top=100,left=100,statusbar=0,toolbar=1'

【讨论】:

    【解决方案2】:

    如果你有 jQuery,你可以这样做......

    javascript:

    $('#select_url').change(function(evnt){ location.href = $(this).val(); });

    html:

    ...

    【讨论】:

      【解决方案3】:

      例如在标题中添加类似的内容

      <script language="JavaScript" type="text/javascript">
      function jumpMenu(targ,selObj,restore){ 
        eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
      }
      </script>
      

      你的选择框看起来像这样

      <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>
      

      【讨论】:

      猜你喜欢
      • 2020-11-04
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      • 2011-12-11
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多