【问题标题】:select option onclick change url or change div选择选项 onclick 更改 url 或更改 div
【发布时间】:2011-10-10 22:24:09
【问题描述】:

所以我想做的是,在我选择第一个选项的那一刻,显示一个 div,如果我选择选项 2 将我重定向到另一个 url。我什至不知道这是否可能! 示例:

<select class="required" id="thechoices">
<option value="">Service Type</option>
<option value="box1">From LAX Airport</option>
<option VALUE="http://www.dma.org/linuxsig/">To The Airport</option>
</select>

<div id="box1"><p>Box 1 stuff...</p></div>

【问题讨论】:

  • 您是否总是只有两个选项,例如“From Airport”和“To Airport”可供选择?

标签: javascript url html onclick onchange


【解决方案1】:
<select id="options" onchange="optionCheck()">
 <option></option>
 <option value="show">Show Div</option>
 <option value="goto">Go To Google</option>
 </select>
<div id="hiddenDiv" style="height:100px;width:300px;border:1px;visibility:hidden;">
I am visible now!</div>

<script type="text/javascript">
    function optionCheck(){
        var option = document.getElementById("options").value;
        if(option == "show"){
            document.getElementById("hiddenDiv").style.visibility ="visible";
        }
        if(option == "goto"){
            window.location = "http://google.com";
        }
    }
</script>

你可以用 jquery 让它更漂亮,但这应该可以。

【讨论】:

  • 你最后一行评论,ciprian 从来没有提到他在使用 jquery :)
【解决方案2】:

有可能

将 onchange 事件处理程序添加到您的选择中。

<select class="required" id="thechoices" onchange="return airportChoice(this)">

将要隐藏的框 ID 的显示设置为隐藏。

   #box1{display:none} //css

这是一个通用函数,它根据参考接收参数,根据您的问题,您可以轻松地从机场选择很多,如果您需要更多选择,只需将以下内容添加到您的每个选择中。

 onchange="return airportChoice(this)" 

JS函数

function airportChoice(n){

  if(n.selectedIndex === 1){
  // show a div (id)  // alert(n.value);
    document.getElementById(n.value).style.display = "block";
   }else if(n.selectedIndex === 2){
     location.href= n.value;  // redirect the user to the url 
   }
    // this last one is not what you ask but for completeness 
    // hide the box div if the first option is selected again
    else if (n.selectedIndex == 0){ // alert(n[1].value);
    document.getElementById(n[1].value).style.display = "none";
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 2018-10-24
    • 2014-01-12
    • 2012-02-19
    • 1970-01-01
    • 2012-12-19
    相关资源
    最近更新 更多