开发者

Set exact option for select-box which generated on the fly with AJAX

My AJAX function generates #parent select-box on the fly. The problem is, i can't set exact option after menu generation

Tried

window.onload = function () {
var parent='1';
$('#paren开发者_Go百科t').val(parent);   
}   

And

$(document).ready(function () {
var parent='1';
$('#parent').val(parent);   
});

no success!. Any suggetions?


Neither onload nor ready will wait for an Ajax request. Move the function to the callback that you pass to the success handler.


Basiclly... You need to be sure that in <option> there is a value. Here is example

(HTML - script.html)

<select id="parent">
    <option value="0">First</option>
    <option value="1">Second</option>
</select>

(JS - index.html)

$(function(){
    $.get('script.html', function(data){
        $('#someDiv').append(data); // appends your select list to some div

        /* after <select> is appended */
        var parent = '1';
        $('#parent').val(parent); // selects "Second" option
    })
})
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜