add attribute to an unknown select option or more

Asked

Viewed 290 times

0

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

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

No option has the Selected attribute set or only the option mentioned above. I don’t know any information previously, because the developer made Generic code for the whole system and kept by default, the first option of each select, with the value coming from the database. I wonder if anyone can help me. I leave fragments of the code. Thank you

$.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 dynamically alter the select. When the user selects an item and one of the selects, I need to add the Selected attribute. But nothing works at all. I tried Rigger, tried to use attr, setAttributes, among others

  • Willian, I confess I was confused about exactly what you wish to do?

  • Sorry. 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, I tried to use attr, setAttributes, among others

  • You can use .val() jQuery, if you know how to select the element select. Thus: $("#id_do_select").val("valor_do_option");

  • By the class that is in HTML I believe you are using the Jquery UI Selectmenu. Check its documentation to learn how to select an item via javascript. https://jqueryui.com/selectmenu/#product-Selection . I believe that using the library method you will be able to force the selected option in the generated interface (select is actually mounted with UL and LI) to reflect in select (which is below the hidden cloths)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.