繁体   English   中英

如何将事件绑定到更改的selectedIndex上 <select>元件?

[英]How can I bind an event on a selectedIndex changed on <select> element?

通过使用jQuery,我如何在以下选择元素上附加事件,以便在索引更改时发出“我已更改!”的警报?

<select id="selCars">
  <option>Volvo</option>
  <option>Saab</option>
  <option>Mercedes</option>
  <option>Audi</option>
</select>

使用onchange事件。 <select>元素的selectionIndex更改时,将触发change事件。

创建元素后执行这段代码。 如果要添加负载,则将其包装在$(document).ready(function(){ ... }) ,缩写为$(document).ready(function(){ ... }) $(function(){...})

$("#selCars").change(function(){
    // Do something...
    //this refers to the `<select>` element
    var newoption = $(this)
    var selectedOption = $("option:selected", this);

    //Example: 
    alert("I have changed!");
})
$("#selCars").change(function() {
    alert("I have changed!");
});
$(function(){  
   $("#selCars").change(function() {
       alert("I have changed!");
   });
}); 
$('#selCars').change(function(){
    alert( "I have changed!" );
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM