How to change the value of a dropdown box from within it's own change function so that the change function is called again
I run some code when a value is selected from a drop-down box:
$('#temp_id').change( function() {
if ($('#temp_id').val() == "temp_val") {
$('#temp_id').val("temp_val2");
}
}):
This code runs ok, but the $('#temp_id').val("temp_val");
line changes the value of the dropdown box without causing t开发者_StackOverflowhe change function to run again, which I need it to. Is there a way to do this?
This should work methinks:
$('#temp_id').change( function() {
if ($('#temp_id').val() == "temp_val") {
$('#temp_id').val("temp_val2");
$('#temp_id').change(); // or $('#temp_id').trigger('change');
}
}):
You just need to explicitly trigger the event again.
精彩评论