简体   繁体   English

动态预选多个选择下拉菜单

[英]pre-select multiple select dropdowns dynamically

I have 6 select boxes with same options.But I want to preselect them depending on the label. 我有6个具有相同选项的选择框。但是我想根据标签预先选择它们。

    $.each(employees, function(key,val) {
                    emp+= '<option value='+val.field+'>'+key+'</option>'; 
                });
           if (data.length < 13) {
                    label_dropdown += '<form name="import_emp_chose" id="import_emp_chose" class="col-md-offset-3">';
                    label_dropdown += '<div class="form-group col-md-9"><label style="color: #00bfff;" class="control-label txtColor text-right import-label-text col-md-3">ATTENDANCE EXCEL FIELDS</label><label style="color: #00bfff;padding-left:14px;" class="control-label txtColor text-left import-label-text col-md-4">ATTENDANCE MAPPING FIELDS</label></div></div>';
                    for(var i=0;i<data.length;i++){ 
                        if ( data[i]!=0 && data[i]!==null) {
                            excel_title = capitalizeFirstLetter(data[i]);
                            label_dropdown += '<div class="form-group col-md-9">';
                            label_dropdown += '<label name="'+data[i]+'" id="'+data[i]+'" class="control-label labeltxt text-right import-label-text col-md-3">'+excel_title+'</label>';
                            label_dropdown += '<div class="col-md-4">';
                            label_dropdown += '<select class="form-control col-md-3 dropdown_field"  name="'+data[i]+'">'+emp+'</select>';           
                            label_dropdown += '</div><span style="color: #ff5050;" class="help-block hidden '+data[i]+'_error">This field already mapped.</span>';        
                            label_dropdown += '</div>';
                        }
                   dosettimeout(i);
                    }
                }
           function dosettimeout(i){
            setTimeout(function(){
             $('.dropdown_field option[value ="'+i+'"]').attr('selected',true);
            }, 2000);
           }

I have the following code. 我有以下代码。 dosettimeout() function is selecting the option but it's selecting the same option for all the select dropdowns. dosettimeout()函数正在选择该选项,但为所有选择下拉列表都选择了相同的选项。 How do I select an option depending on the label? 如何根据标签选择选项?

You didnt close the bracket in the option selector. 您没有在选项选择器中合上支架。 It should be: 它应该是:

$('.dropdown_field option[value ="'+i+'"]').attr('selected',true);

Does that fix it? 这样可以解决吗?

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

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