【问题标题】:Javascript drop down menu onchange event only for one fuctionJavascript 下拉菜单 onchange 事件仅适用于一项功能
【发布时间】:2015-10-10 21:15:28
【问题描述】:

这就是我想要实现的:在下面的函数中,当下拉菜单发生变化时,函数将从文本框中获取值并执行查找。我不想在<select> 标签中添加onchange,因为下拉菜单中还有其他功能。我可以只在下面的这个函数中添加一个 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{

                }       
            } 

【问题讨论】:

  • 不清楚你在问什么
  • 除了将“onchange”放在下拉菜单
  • 请先发布您的“相关”HTML 代码。

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


【解决方案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>

【讨论】:

  • 谢谢!但是在这种情况下,我应该在哪里/如何添加 addEventListener?我假设如果我将它添加到下拉菜单中,每次菜单更改时都会触发function(e)?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多