简体   繁体   English

为使用AJAX动态生成的选择框设置精确选项

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

My AJAX function generates #parent select-box on the fly. 我的AJAX函数动态生成#parent选择框。 The problem is, i can't set exact option after menu generation 问题是,我无法在菜单生成后设置准确的选项

Tried 试着

window.onload = function () {
var parent='1';
$('#parent').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. onloadready都不会等待Ajax请求。 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. 基本...你需要确保在<option>有一个值。 Here is example 这是一个例子

(HTML - script.html) (HTML - script.html)

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

(JS - index.html) (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
    })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM