How to create dropdown list dynamically using jQuery?

前端 未结 3 431
一整个雨季
一整个雨季 2020-11-30 02:57

How do you create a dropdown list dynamically using jQuery? By dropdown list, I mean a

提交评论

  • 2020-11-30 03:51

    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
    
    0 讨论(0)
  • 2020-11-30 03:53

    In its simplest form,

    var opt = "<option> -- Select -- </option>";
    
    $(opt).wrap('<select />');
    
    $('#some-container-div').html(opt);
    
    0 讨论(0)
  • 提交回复
    热议问题