Javascript 在下拉更改时运行 jQuery 函数

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

Run jQuery function on drop down change

javascriptjquery

提问by user547794

I wrote a jQuery function that currently runs on Click Event. I need to change it so that it runs when a drop down box (Select- Option) value is changed. Here is my code:

我编写了一个当前在 Click 事件上运行的 jQuery 函数。我需要更改它,以便它在下拉框(选择选项)值更改时运行。这是我的代码:

<form id="form1" name="form1" method="post" action="">
  <label>
    <select name="otherCatches" id="otherCatches">
      <option value="*">All</option>
    </select>
  </label>
</form>


$("#otherCatches").click(function() {
    $.ajax({
        url: "otherCatchesMap.php>",
        success: function(msg) {
            $("#results").html(msg);
        }
    });
});

回答by Pan Thomakos

Use change()instead of click():

使用change()代替click()

jQuery(document).ready(function(){
  $("#otherCatches").change(function() {
    $.ajax({
     url: "otherCatchesMap.php>",
     success: function(msg){
       $("#results").html(msg);
     }
   });
  });
});

回答by Robin

http://api.jquery.com/change/

http://api.jquery.com/change/

$(function() {
    $("#otherCatches").change(function() {
       $(this).val() // how to get the value of the selected item if you need it
    });
});

回答by Jith


$("#otherCatches").change(function () {
    //// Your code        
});