开发者

How to change the value of a dropdown box from within it's own change function so that the change function is called again

开发者 https://www.devze.com 2023-01-23 22:41 出处:网络
I run some code when a value is selected from a drop-down box: $(\'#temp_id\').change( function() { if ($(\'#temp_id\').val() == \"temp_val\") {

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消