在下拉更改时运行jQuery函数

use*_*794 14 javascript jquery

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

<form id="form1" name="form1" method="post" action="">
  <label>
    <select name="otherCatches" id="otherCatches">
      <option value="*">All</option>
    </select>
  </label>
</form>
Run Code Online (Sandbox Code Playgroud)
$("#otherCatches").click(function() {
    $.ajax({
        url: "otherCatchesMap.php>",
        success: function(msg) {
            $("#results").html(msg);
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

Pan*_*kos 33

使用change()而不是click():

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

  • .change()非常适合<select>列表 (2认同)

Rob*_*bin 22

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

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


Jit*_*ith 11


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