Javascript下拉菜单onchange事件仅用于一个功能

时间:2015-10-10 21:15:29

标签: javascript html events drop-down-menu onchange

这就是我想要实现的目标:在下面的函数中,当下拉菜单更改时,该函数将从文本框中获取值并执行查找。我不想在 <select id="types"> <option value ="food">food</option> <option value ="drinks">drinks</option> <select> function lookup(){ var keyword = input.value;//get value from text box var type = document.getElementById("types").value;//get value from drop down menu if (search=="food"){ //perform look up //I 'm trying to make it do another search when the type from the drop down menu changes }else{ } } 标记中添加一个onchange,因为还有其他功能链接到下拉菜单。我可以在下面的此功能中添加更改吗?非常感谢提前!

Initial_Exam

1 个答案:

答案 0 :(得分:1)

您可以添加像这样的事件处理程序

window.addEventListener('change', function(e) {
    if (e.target.id == "types") {
        lookup(e.target);
    }
});

function lookup(){
  
    alert("here now...");
    return ; // temp exit
  
    
    var keyword = input.value;//get value from text box
    var type = document.getElementById("types").value; //get value from drop down menu
    if (search=="food"){
        //perform look up
        //I 'm trying to make it do another search when 
        //the type from the drop down menu changes
    }else{

    }       
} 
<select id="types"> 
    <option value ="food">food</option>
    <option value ="drinks">drinks</option>
<select>