Javascript下拉菜单onchange事件只针对一个函数

Javascript drop down menu onchange event only for one fuction

本文关键字:一个 函数 onchange 下拉菜单 事件 Javascript      更新时间:2023-09-26

这就是我想要实现的:在下面的函数中,当下拉菜单更改时,该函数将从文本框中获取值并执行查找。我不想添加一个onchange到<select>标签,因为有其他功能链接到下拉菜单。我可以只对下面这个函数添加一个on change吗?提前感谢!

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

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

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>