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
Click event on Dropdown items - jquery
提问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");
}
?
?
回答by Umair Noor
回答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();}
});
});