JQuery create new select option

前端 未结 6 1360
遥遥无期
遥遥无期 2020-12-29 19:46

I have the below functions in regular JavaScript creating select options. Is there a way I can do this with jQuery without having to use the form object? Perhaps storing the

相关标签:
6条回答
  • 2020-12-29 19:46

    What about

    var option = $('<option/>');
    option.attr({ 'value': 'myValue' }).text('myText');
    $('#county').append(option);
    
    0 讨论(0)
  • 2020-12-29 19:50

    Something like:

    function populate(selector) {
      $(selector)
        .append('<option value="foo">foo</option>')
        .append('<option value="bar">bar</option>')
    }
    
    populate('#myform .myselect');
    

    Or even:

    $.fn.populate = function() {
      $(this)
        .append('<option value="foo">foo</option>')
        .append('<option value="bar">bar</option>')
    }
    
    $('#myform .myselect').populate();
    
    0 讨论(0)
  • 2020-12-29 19:51

    If you need to make single element you can use this construction:

    $('<option/>', {
        'class': this.dataID,
        'text': this.s_dataValue
    }).appendTo('.subCategory');
    

    But if you need to print many elements you can use this construction:

    function printOptions(arr){
        jQuery.each(arr, function(){
            $('<option/>', {
                'value': this.dataID,
                'text': this.s_dataValue
            }).appendTo('.subCategory');
        });
    }
    
    0 讨论(0)
  • 2020-12-29 19:51

    This is confusing. When you say "form object", do you mean "<select> element"? If not, your code won't work, so I'll assume your form variable is in fact a reference to a <select> element. Why do you want to rewrite this code? What you have has worked in all scriptable browsers since around 1996, and won't stop working any time soon. Doing it with jQuery will immediately make your code slower, more error-prone and less compatible across browsers.

    Here's a function that uses your current code as a starting point and populates a <select> element from an object:

    <select id="mySelect"></select>
    
    <script type="text/javascript>
    
    function populateSelect(select, optionsData) {
        var options = select.options, o, selected;
        options.length = 0;
        for (var i = 0, len = optionsData.length; i < len; ++i) {
            o = optionsData[i];
            selected = !!o.selected;
            options[i] = new Option(o.text, o.value, selected, selected);
        }
    }
    
    var optionsData = [
        {
            text: "Select a city / town in Sweden",
            value: ""
        },
        {
            text: "Melbourne",
            value: "Melbourne",
            selected: true
        }
    ];
    
    populateSelect(document.getElementById("mySelect"), optionsData);
    
    </script>
    
    0 讨论(0)
  • 2020-12-29 19:58

    How about

    $('#county').append(
        $('<option />')
            .text('Select a city / town in Sweden')
            .val(''),
        $('<option />')
            .text('Melbourne')
            .val('Melbourne')
    );
    
    0 讨论(0)
  • 2020-12-29 20:09

    A really simple way to do this...

    // create the option
    var opt = $("<option>").val("myvalue").text("my text");
    
    //append option to the select element
    $(#my-select).append(opt);
    

    This could be done in lots of ways, even in a single line if really you want to.

    0 讨论(0)
提交回复
热议问题