How can I bind an event on a selectedIndex changed on <select> element?
By using jQuery how do I attach an event on the following select element so that on index change it alerts "I have changed!"?
<select id="selCars">
<option>Volvo</option>
<option>Saab</option>
<option>Mercedes</op开发者_如何学运维tion>
<option>Audi</option>
</select>
Use the onchange
event. When the selectionIndex
of the <select>
element changes, the change
event is fired.
Execute this piece of code after the element is created. If you want to add the on load, wrap it in a $(document).ready(function(){ ... })
, abbreviated by $(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!");
})
$(function(){
$("#selCars").change(function() {
alert("I have changed!");
});
});
$("#selCars").change(function() {
alert("I have changed!");
});
$('#selCars').change(function(){
alert( "I have changed!" );
})
精彩评论