Javascript 选择选项onclick change url或change div

Javascript 选择选项onclick change url或change div,javascript,url,html,onclick,onchange,Javascript,Url,Html,Onclick,Onchange,所以我想做的是,当我选择第一个选项,显示一个div,如果我选择选项2将我重定向到另一个url。我甚至不知道这是否可能! 样本: 服务类型 从洛杉矶机场 去机场 盒子1的东西 节目组 去谷歌 我现在看得见了! 函数optionCheck(){ var option=document.getElementById(“options”).value; 如果(选项==“显示”){ document.getElementById(“hiddenDiv”).style.visibility=“visib

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


服务类型
从洛杉矶机场
去机场
盒子1的东西


节目组
去谷歌
我现在看得见了!
函数optionCheck(){
var option=document.getElementById(“options”).value;
如果(选项==“显示”){
document.getElementById(“hiddenDiv”).style.visibility=“visible”;
}
如果(选项==“转到”){
window.location=”http://google.com";
}
}
您可以使用jquery使其更加美观,但这应该是可行的。

是的,这是可能的

将onchange事件处理程序添加到您的select

<select class="required" id="thechoices" onchange="return airportChoice(this)">
这是一个通用函数,它接收基于引用的参数,基于您的问题,这样您就可以轻松地从机场获得很多选择。如果您需要更多选择,只需在每个选择中添加以下内容

 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";
    }
  }

你最后一行的评论,ciprian从来没有提到过他在使用jquery:)你总是只有两个选项,比如选择“从机场”和“到机场”吗?
   #box1{display:none} //css
 onchange="return airportChoice(this)" 
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";
    }
  }