开发者

How to create dropdown list dynamically using jQuery?

开发者 https://www.devze.com 2023-02-07 04:15 出处:网络
How do you create a dropdown list dynamically using jQuery?By dropdown list, I mean a <select> with its a开发者_JS百科ssociated <option> values.Just create the elements like any element.

How do you create a dropdown list dynamically using jQuery? By dropdown list, I mean a <select> with its a开发者_JS百科ssociated <option> values.


Just create the elements like any element.

Example:

var data = {
    'foo': 'bar',
    'foo2': 'baz'
}


var s = $('<select />');

for(var val in data) {
    $('<option />', {value: val, text: data[val]}).appendTo(s);
}

s.appendTo('body'); // or wherever it should be


In its simplest form,

var opt = "<option> -- Select -- </option>";

$(opt).wrap('<select />');

$('#some-container-div').html(opt);


http://hungred.com/how-to/tutorial-jquery-select-box-manipulation-plugin/

0

精彩评论

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