jquery function on dropdown select

2020-05-20 07:12发布

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

<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.

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

6条回答
三岁会撩人
2楼-- · 2020-05-20 07:31

use

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

hope this helps....

查看更多
戒情不戒烟
3楼-- · 2020-05-20 07:34
    $(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.

查看更多
beautiful°
4楼-- · 2020-05-20 07:36
$(document).ready(function() {
  $("select[name='dropdown']").change(function() {
     alert($(this).val());
  });
});
查看更多
地球回转人心会变
5楼-- · 2020-05-20 07:38

Try this demo 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

<select name="dropdown" size=1>
    <option value="1">option 1</option>
    <option value="2">option 2</option>
</select>​
查看更多
狗以群分
6楼-- · 2020-05-20 07:45

Try this one, Demo on JsFiddle

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

});
查看更多
叛逆
7楼-- · 2020-05-20 07:54

You need to use change function

$('select[name=dropdown]').change(function() {
    alert($(this).val());
});
查看更多
登录 后发表回答