SELECT2 is not returning pre-selected item value

Asked

Viewed 27 times

-3

When changing a Select2 option, it is returning all the data correctly, but when I go back to the previous option (which was already selected), it is not searching the values correctly.

Exemplifying better:

I have a "change" form with a Select2 that comes pre-selected with the current value (value01) pulled from the database. So far so good, but when I give a "change" in Lect2 (valu02), it has to change the "price" field in the same form. This part, quiet, I’m doing perfectly. But when I change Select2 again to (value01) it should return the previous price, but this value returns "Undefined", as if it had used a "cache", even though I have the cache as false.

Code of my Lect2:

(function($){
    $.fn.selects = function(urlcontent, tamanho='1', holder=''){
        $(this).select2( {
            width: '100%',
            selectionTitleAttribute: false,
            minimumInputLength: tamanho,
            language: "pt-BR",
            cache: false,
            escapeMarkup: function (markup) {
                return markup;
            },
            placeholder: holder,
            selectOnClose: true,
            ajax: {
                url: urlcontent,
                type: "post",
                dataType: 'json',
                delay: 250,
                data: function (params) {
                   return {
                      searchTerm: params.term // search term
                   };
                },
                processResults: function (response) {
                   return {
                      results: response
                   };
                }
            }
        });
    };
})(jQuery);

Call from the Select2:

$('#id_produto').selects("classes/self_preco.php",0,"Selecione...");
$('#id_produto').change(function(e) {
    $('#preco').val($(this).select2('data')[0]['preco']);
});

self_preco.php

while($reply = mysqli_fetch_object($con)){
    $response[] = array(
        "id" => $reply->iddado,
        "text" => $reply->produtodetalhe,
        "preco" => floatForce($reply->preco)
    );
}

echo json_encode($response);
exit();
  • Hello Evandro. It’s important [Edit] and add a [mcve] of the problem, with step by step what you have already done and explain clearly and objectively where you have failed. Do not delete and do not repeat the question, just edit and wait for the reopening process. To take better advantage of the site, understand and avoid closures is worth reading the Stack Overflow Survival Guide in English. Thank you for understanding.

No answers

Browser other questions tagged

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