add attribute to one or more unknown option

0

I have a code that dynamically defines the input fields. In a context, I have several selects being set, and I do not know the name or its id. Selects inputs and selects options are defined dynamically, via ajax. The first option is selected by default

<option value="">Selecione um valor...</option>

No option has the selected attribute set or just the option mentioned above. I do not know any information previously, because the developer made generic code for the whole system and maintained by default, the first option of each select, with the value coming from the database. I wonder if anyone can help me. I leave below fragments of code. Thanks

$.ajax({
    url: url.properties,
    dataType: 'json',
    data: {category: category}
})
.done(function(properties) {
    window.categoryProperties = properties;
    loadProperties();
})

function loadProperties() {
if ($.isEmptyObject(window.categoryProperties) === false) {
    var html = '';

    // metadados
    html += '<div class="form-group row">';
    html += '<label for="inputEmail3" class="col-sm-2 col-form-label">Caracter&iacute;sticas</label>';
    html += '<div class="col-sm-10">';
    html += '<label for="inputEmail3" class="col-form-label">Valor</label>';
    html += '</div>';
    html += '</div>';

    $.each(window.categoryProperties, function(index, property) {
        html += '<div class="form-group row">';
        html += '<label for="inputEmail3" class="col-sm-2 col-form-label">' + property.description + '<span class="required">*</span></label>';
        html += '<div class="col-sm-10">';
        html += createFieldPropertyValue(property.id, property.type, property.options, property.options.selected);
        html += '</div>'
        html += '</div>'

    });
} else {
    var html;
    html += '<div class="form-group row">';
    html += '<label for="formGroupExampleInput">Escolha uma categoria</label>';
    html += '</div>'
}

// método substitudo para inserir as div's
$('.selected_feature_data').html(html);
$('#tblProperties').find('.select2').css('width', '100%');
$('#tblProperties').find('.select2').select2({language: "pt-BR"});
}

function createFieldPropertyValue(property, type, options) {
switch (type) {
    case 'text':
        field = '<input type="text" name="TB_PRP_TIP_CPO_VLR_CPO_OPC[' + property + ']" class="form-control requiredField">';
        break;
    case 'select':
        var classSelect2 = '';
        if (options.length > 5) {
            classSelect2 = 'select2';
        }

        field = '<select name="TB_PRP_TIP_CPO_VLR_CPO_OPC[' + property + ']" class="form-control requiredField ui-selectmenu ' + classSelect2 + '">';
        field += '<option value="">Selecione uma opção...</option>';
            $.each(options, function(index, val) {
                field += '<option value="' + val.id + '">' + val.description + '</option>';
            });
        field += '</select>';
        break;
    case 'number':
        field = '<input type="text" name="TB_PRP_TIP_CPO_VLR_CPO_OPC[' + property + ']" class="form-control requiredField mask-number" value="">';
        break;
}

return field;
}

I need to change the select dynamically. When the user selects an item and one of the selects, I need to add the selected attribute. But nothing works. I tried trigger, tried to use attr, setAttributes, among others

    
asked by anonymous 18.06.2018 / 18:16

0 answers