简体   繁体   中英

pre-select multiple select dropdowns dynamically

I have 6 select boxes with same options.But I want to preselect them depending on the label.

    $.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. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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