如何根据值调用多个函数。 [英] How to I call multiple function on based of values.

查看:148
本文介绍了如何根据值调用多个函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Selectbox,它由4个值组成。我想调用特定值的函数。在按钮上我们只能调用函数onClick。所以我想要的是,如果选择值为1,则在按钮点击时调用函数1,依此类推。



我尝试过:



请为我推荐一些代码。



I have One Selectbox which consist of 4 values. I want to call function for specific value. On button where we can call only on function onClick. So what I want is like if value is 1 selected then call function 1 on button click and so on.

What I have tried:

Please suggest me some code.

<pre><td>
  <div class="dropdown">
   <select id="myDropdown" class="dropbtn" data-native-menu="true"> 
                        <option selected="selected">Search▼</option>
                        <option value="Alert" id="Alert">Alert</option>
                        <option value="Incident Report">IR</option>
                        <option value="Site">Site</option>
                        <option value="Device">Devices</option>                     
                    </select></div>
  </td>





这是下拉菜单



this is the dropdown

<pre><td> 
<button type="submit" onclick="SearchContext()" id="searchbutton" style="height:50px; width:150px" class="searchbtn">Search Now</button>
</td>



这是按钮


and this is the button

推荐答案

SearchContext ,获取所选值并使用开关来决定调用哪个函数:

In SearchContext, take the selected value and use a switch to decide which function to call:
function SearchContext() {
    var selectedValue = document.getElementById("myDropdown").value;
    switch (selectedValue) {
        case "Alert":
            alertButtonClicked();
            break;
        case "Incident Report":
            irButtonClicked();
            break;
        case "Site":
            siteButtonClicked();
            break;
        case "Device":
            deviceButtonClicked();
            break;
    }
}

用实际的函数名替换 ... ButtonClicked 名称。

Replace the ...ButtonClicked names with your actual function names.


这篇关于如何根据值调用多个函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆