Im filling a 'select' with multiple options with JSON/AJAX javascript and inside "document ready".. Right after i will set the value of the select with:

$('#dropdown').val('99');

Nothing happens, but the function works fine if i place it in the console?

有帮助吗?

解决方案

You likely need to place $('#dropdown').val('99'); in the success callback of .ajax.

e.g.,

$.ajax({
    url: 'google.com',
    success: function(){
        $('#dropdown').val('99');
    }
});

$.ajax is asynchronous, meaning any code directly after it will run before the ajax request is complete. It's likely you're trying to set the value to 99 before it's in the dropdown.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top