문제

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