Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
Select multiple options by default using chosen plugin

I am using a chosen plugin for my multi-select list. Here I want to select various options by default. I tried to follow some responses from this thread, and ended up using:

$("#laptop_product").val("Macbook 12").trigger('change');
$('#laptop_product').trigger('chosen:updated');

where laptop_product is the id of the select element:

<select name="products" multiple class="standardSelect" id="laptop_product" onchange="tableRefresh()" tabindex="5">

and Macbook 12 is one of the values in the dropdown list. However, this is not working and the options don't appear on the website.

The value of each option should be the same as the text, this is done in jQuery and extracted from other json file:

var laptop_data = jQuery('#laptop_product');
    jQuery.each(data, function (key, cat) {
        var laptop_keys = jQuery('<optgroup>',{label:key});
                jQuery.each(cat,function(key,item) {
                jQuery("<option/>",{value:key,text:key})
                .appendTo(laptop_keys);
        });
        laptop_keys.appendTo( laptop_data );
    });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Without a proper example, I can only suggest the following.

jQuery(function($) {
  var laptop_data = $('#laptop_product');
  $.each(data, function(key, cat) {
    var laptop_keys = $('<optgroup>', {
      label: key
    });
    $.each(cat, function(index, item) {
      $("<option/>", {
          value: index,
          text: item
        })
        .prop("selected", (item == "Macbook 12"))
        .appendTo(laptop_keys);
    });
    laptop_keys.appendTo(laptop_data);
  });
  laptop_data.chosen();
});

This builds the Select structure first. If a specific item matches, selected, will be set as a property.

It is a better practice to not re-use variable names inside of loops. It becomes ambiguous and may not get the proper results.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda