How can I detect in jQuery when any option in a select tag has been changed?
For example [pseudocode]:
event handler function -> option changed(){
//do some stuff
}
EDIT:
I have the following select:
<select id = "selectattribute">
<OPTION VALUE="0">Choose Attribute</OPTION>
<?php echo($options);?>
</select>
I am trying this in jQuery:
$(document).ready(function() {
$("#selectattribute").change(function() {
alert('Handler for .change() called.');
});
});
Nothing happens...the function isn;t triggering.
EDIT: it works when I use $("#select")
instead of $("#selectattribute"开发者_StackOverflow社区)...can you not apply it to particular select tags?
From jQuery docs regarding the change handler:
<form>
<input class="target" type="text" value="Field 1" />
<select class="target">
<option value="option1" selected="selected">Option 1</option>
<option value="option2">Option 2</option>
</select>
</form>
<div id="other">
Trigger the handler
</div>
The event handler can be bound to the text input and the select box:
$('.target').change(function() {
alert('Handler for .change() called.');
});
Your Code works in Firefox using the mouse, see this fiddle.
However using the keyboard is a different matter as per this bug report
To work around this I've added a key up handler to remove focus from the element then re-focus, as per this fiddle
If the change event is not triggering when using the mouse, check the options rendered by your php code. Look for structures that might be "breaking" the select tag.
$("select").change(function() {})
Maybe there is another tag which id is named "selectattribute".The tag id should be unique.If there is more than one id,jQuery will use the first one founded.
Try bind:
$("#selectattribute").bind('change', function() {
alert('Handler for .change() called.');
});
or change function in jQUery:
$("#selectattribute").change(
function() {
alert('Handler for .change() called.');
}
);
Looks like your JQuery is not loaded,check first that it is loaded
//case if mini jQueryis not used replace with window.jQuery
if (window.$) {
console.debug('jQuery is loaded ');
} else {
console.debug('jQuery is not loaded ');
}
精彩评论