How to select Option value with Ajax data return?

1

I have the following HTML code:

<select id="priorities-info" class="form-control selectpicker" data-live-search="true" disabled="" tabindex="-98">
    <option value="1" data-tokens="Alta">Alta</option>
    <option value="2" data-tokens="Média">Média</option>
    <option value="3" data-tokens="Baixa">Baixa</option>
</select>

I'm trying to compare data-tokens like this:

$('#priorities-info').each(function() {
    $(this).attr('selected', this.dataset.tokens == responseData.Type);
});

But I can not, does anyone have any ideas?

    
asked by anonymous 30.11.2016 / 13:13

1 answer

2
In the selector I believe you should include the options and check if the value of the data-tokens attribute is the same as that returned by ajax:

$('#priorities-info option').each(function() {
  if ( $(this).attr('data-tokens') == 'Média')
    $(this).prop('selected', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><selectid="priorities-info" class="form-control selectpicker" data-live-search="true" tabindex="-98">
    <option value="1" data-tokens="Alta">Alta</option>
    <option value="2" data-tokens="Média">Média</option>
    <option value="3" data-tokens="Baixa">Baixa</option>
</select>
    
30.11.2016 / 13:34