最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - drop down menu with onclick javascript functions - Stack Overflow

programmeradmin2浏览0评论

I'm trying to create a drop down menu that calls a JavaScript function when clicked. My drop down menu is like this:

<select name = "navyOp"> 
    <option selected = "selected">Select a Navy Op Area</option>
    <option value = "AN01">AN01</option>
    <option value = "AN02">AN02</option>
    <option value = "AN03">AN03</option>
</select>

So for each of this options, I want a specific JavaScript function to be called. Is there a simple way to do this?

I'm trying to create a drop down menu that calls a JavaScript function when clicked. My drop down menu is like this:

<select name = "navyOp"> 
    <option selected = "selected">Select a Navy Op Area</option>
    <option value = "AN01">AN01</option>
    <option value = "AN02">AN02</option>
    <option value = "AN03">AN03</option>
</select>

So for each of this options, I want a specific JavaScript function to be called. Is there a simple way to do this?

Share Improve this question edited Jul 22, 2014 at 12:31 Lukasz Koziara 4,3205 gold badges34 silver badges44 bronze badges asked Jun 16, 2014 at 19:25 CarmenCarmen 351 gold badge1 silver badge4 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

You can attach an event listener to select element:

window.test = function(e) {
  if (e.value === 'AN01') {
    console.log(e.value);
  } else if (e.value === 'AN02') {
    console.log(e.value);
  } else if (e.value === 'AN03') {
    console.log(e.value);
  }
}
<select name="navyOp" onchange="test(this);">
  <option selected="selected">Select a Navy Op Area</option>
  <option value="AN01">AN01</option>
  <option value="AN02">AN02</option>
  <option value="AN03">AN03</option>
</select>

You can try this

html change or edit:

<select name = "navyOp" id = "navyOp"> 

JavaScript:

    document.getElementById("navyOp").onchange = function()
{

    if(this.value === "AN01")
    {
        //do this function
        alert("an01");
    }
    else if(this.value == "AN02")
    {
        //do this function
        alert("an02");
    }
    else if(this.value == "AN03")
    {
        alert("an03");
    }
};

You could add a function that gets called everytime you change the value :

HTML

<select name = "navyOp" onChange="myFunction()"> 
    <option selected = "selected">Select a Navy Op Area</option>
    <option value = "AN01">AN01</option>
    <option value = "AN02">AN02</option>
    <option value = "AN03">AN03</option>
</select>

JS

var myDropdown=document.getElementsByName('navyOp')[0];

function myFunction(){
    alert('option changed : '+myDropdown.value);
}

JS Fiddle

One way to do this is

function expand(s)
{
  var td = s;
  var d = td.getElementsByTagName("div").item(0);

  td.className = "menuHover";
  d.className = "menuHover";
}

You have to do this for each of your options menu. Hope this answers your question

发布评论

评论列表(0)

  1. 暂无评论