简体   繁体   English

无法将选项动态添加到选择下拉列表

[英]Cant Append options to a select dropdown dynamically

i am creating a javascript function which when clicked will create two drop downs and one text box and will append the html to the container div 我正在创建一个javascript函数,单击该函数将创建两个下拉列表和一个文本框,并将html附加到容器div

but the problem is that, somehow it can't append the oprions to the two drop downs. 但问题在于,它无法以某种方式将oprions附加到两个下拉菜单中。

Here's my script 这是我的剧本

var i = 0;
function add_relation()
{
    i = i + 1;
    var condition = $('<select />', {
                             'class'  : 'relation-select',
                             'id'     : 'condition_' + i,
                             'name'   : 'condition[]'
                         });

    var key = $('<select />', {
                             'class'  : 'relation-select',
                             'id'     : 'key_' + i,
                             'name'   : 'key[]'
                         });

    $("#key_"+i).append('<option value="">Select Key</option>' + 
                        '<option value="product_title">Title</option>' + 
                        '<option value="type">Type</option>' +
                        '<option value="product_price">Price</option>' +
                        '<option value="product_weight">Weight</option>');

    $("#condition_"+i).append('<option value="">Select Condition</option>'+
                        '<option value="is_equal_to">Is Equal To</option>'+
                        '<option value="is_greater_than">Is Greater Than</option>'+
                        '<option value="is_less_than">Is Less Than</option>'+
                        '<option value="starts_with">Starts With</option>'+
                        '<option value="ends_with">Ends With</option>'+
                        '<option value="contains">Contains</option>');

    var left = $('<div />', {'class' : 'relation-left','id'    : 'relation_' + i})
                    .append(condition)
                    .append(key)
                    .append($('<input />', {
                                 'class'  : 'relation-input',
                                 'id'     : 'constraint_' + i,
                                 'type' : 'text',
                                 'name'   : 'constraint[]',
                                 'data-provide' : 'typeahead',
                                 'data-items' : '2'
                             }));

    var right = $('<div />', {'class' : 'relation-right',
                            html : $('<buton />', 
                            {
                                'class' : 'btn',
                                'name' : 'btn[]',
                                'value': 'X'})
                            });

    var parent = $('<div />', {'class' : 'relation-parent_' + i})
                    .append(left)
                    .append(right);
    $("#relation_container").append(parent);
}

You are creating condition and key objects for select box. 您正在为选择框创建conditionkey对象。 Use same objects instead of using id as select boxes not created yet so you cannot find them using id : 使用相同的对象而不是使用id作为尚未创建的选择框,因此无法使用id找到它们:

var i = 0;
    function add_relation()
    {
        i = i + 1;
        var condition = $('<select />', {
                                 'class'  : 'relation-select',
                                 'id'     : 'condition_' + i,
                                 'name'   : 'condition[]'
                             });

        var key = $('<select />', {
                                 'class'  : 'relation-select',
                                 'id'     : 'key_' + i,
                                 'name'   : 'key[]'
                             });
        //here use object directly instead of id
        $(key).append('<option value="">Select Key</option><option value="product_title">Title</option><option value="type">Type</option><option value="product_price">Price</option><option value="product_weight">Weight</option>');
        $(condition).append('<option value="">Select Condition</option>'+
                                                        '<option value="is_equal_to">Is Equal To</option>'+
                                                        '<option value="is_greater_than">Is Greater Than</option>'+
                                                        '<option value="is_less_than">Is Less Than</option>'+
                                                        '<option value="starts_with">Starts With</option>'+
                                                        '<option value="ends_with">Ends With</option>'+
                                                        '<option value="contains">Contains</option>');                           
        var left = $('<div />', {'class' : 'relation-left','id'    : 'relation_' + i}).append(condition).append(key).append($('<input />', {
                                 'class'  : 'relation-input',
                                 'id'     : 'constraint_' + i,
                                 'type' : 'text',
                                 'name'   : 'constraint[]',
                                 'data-provide' : 'typeahead',
                                 'data-items' : '2'

                             }));


        var right = $('<div />', {'class' : 'relation-right',html : $('<buton />', 
                                                                            {
                                                                            'class' : 'btn',
                                                                            'name' : 'btn[]',
                                                                            'value': 'X'})
                                                                    });


        var parent = $('<div />', {'class' : 'relation-parent_' + i}).append(left).append(right);
    $("#relation_container").append(parent);
    }

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

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