Javascript 下拉项上的单击事件 - jquery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12154329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:15:00  来源:igfitidea点击:

Click event on Dropdown items - jquery

javascriptjquery

提问by Shaggy

I want to fire click event when selecting elements from drop down list items. each click event should be diiferent depend on the value of element in drop down.

我想在从下拉列表项中选择元素时触发点击事件。每个点击事件应该是不同的,取决于下拉元素的值。

 <select id="cmbMoreFunction" name="cmbMoreFunction" multiple="multiple">
            <option value="0">ATM Event Status</option>
            <option value="1">Statistics</option>
 </select>

If I Click on "ATM Event Status" Only its specific click event should get fired.

如果我单击“ATM 事件状态”,则应仅触发其特定的单击事件。

I tried this ..but doesnt work

我试过这个..但不起作用

 $('#cmbMoreFunction select option').click(function() 
 { 
    //Depend on Value i.e. 0 or 1 respective function gets called. 
 });

Basically I just dont want another BUTTON on my page for catch all the value and then fire event.

基本上我只是不想在我的页面上使用另一个按钮来捕获所有值然后触发事件。

回答by Asciiom

Use the change handler on the select, read it's value, and decide what to do then:

在选择上使用更改处理程序,读取它的值,然后决定要做什么:

$('#cmbMoreFunction').change(function() 
{ 
    var selectedValue = parseInt(jQuery(this).val());

    //Depend on Value i.e. 0 or 1 respective function gets called. 
    switch(selectedValue){
        case 0:
            handlerFunctionA();
            break;
        case 1:
            handlerFunctionB();
            break;
        //etc... 
        default:
            alert("catch default");
            break;
    }
});

function handlerFunctionA(){
    alert("do some stuff");    
}

function handlerFunctionB(){
    alert("Do some other stuff");
}

?

?

Here is a working fiddle example

这是一个工作小提琴示例

回答by Umair Noor

check this JsFiddle

检查这个JsFiddle

Simple use this code JQuery:

简单使用此代码 JQuery:

$('#cmbMoreFunction').click(function(v){
  console.log(v.target.value);
  });

Hopefully your problem will solved.

希望你的问题会得到解决。

回答by sushil bharwani

A option element will not have a click event. Click event is associated with Select element only so you will have to apply it to select dropdown. In the function that fires on click event of select dropdown, you can then check individual options and apply your logic.

选项元素不会有点击事件。单击事件仅与 Select 元素相关联,因此您必须将其应用于选择下拉列表。在选择下拉菜单的单击事件触发的函数中,您可以检查各个选项并应用您的逻辑。

回答by Priya-Systematix

You can also use javascript. eg.

您也可以使用 javascript。例如。

$document.ready(function(){
$('#cmbMoreFunction').change(function(){

var dataValue=document.getElementById('cmbMoreFunction').value;
if(dataValue==0){call functionFirst();}
else{call functionSecond();}
});
});