jQuery 下拉选择上的jquery函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10481503/
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-26 09:24:27  来源:igfitidea点击:

jquery function on dropdown select

jquerydrop-down-menu

提问by user1022585

I need to run a jquery function if a certain dropdown option is selected.

如果选择了某个下拉选项,我需要运行 jquery 函数。

<select name=dropdown size=1>
    <option value=1>option 1</option>
    <option value=2>option 2</option>
</select>

I have the commands in the function ready, Im just not sure on how to make it so if option 2 is currently active in the dropdown, then run the function.

我已准备好函数中的命令,我只是不确定如何制作它,因此如果选项 2 当前在下拉列表中处于活动状态,则运行该函数。

(dropdown doesnt have a submit button, i want it ran when the user highlights the option)

(下拉菜单没有提交按钮,我希望它在用户突出显示选项时运行)

回答by Tats_innit

Try this demohttp://jsfiddle.net/JGp9e/

试试这个演示http://jsfiddle.net/JGp9e/

This will help, have a nice one!

这会有所帮助,祝你好运!

code

代码

$('select[name="dropdown"]').change(function(){

    if ($(this).val() == "2"){
        alert("call the do something function on option 2");
     }        
});?

HTML

HTML

<select name="dropdown" size=1>
    <option value="1">option 1</option>
    <option value="2">option 2</option>
</select>?

回答by Adil

Try this one, Demo on JsFiddle

试试这个,JsFiddle 上的演示

$('select[name="dropdown"]').change(function() {
    alert($(this).val());

});

回答by Sudhir Bastakoti

$(document).ready(function() {
  $("select[name='dropdown']").change(function() {
     alert($(this).val());
  });
});

回答by Philemon philip Kunjumon

use

$("#idofselectbox").change(function(){
// your code here
});

hope this helps....

希望这可以帮助....

回答by antyrat

You need to use changefunction

您需要使用change功能

$('select[name=dropdown]').change(function() {
    alert($(this).val());
});

回答by Saifuddin Sarker

    $(document).ready(function() {
      $("select[name='dropdown']").change(function() {
         if($(this).val()==2){
             //do what u want here
           }//u can check your desired value here
     });
   });

I think you want this.

我想你想要这个。