Assign value using val(value) or attr('value', value) does not work

Asked

Viewed 474 times

0

I’m using the typeahead, which works as follows, every text I type in the field it takes the value of the field, goes to a database, searches and creates a dropdown with the values found.

It works perfectly, but I wanted as soon as the enter button was pressed the field completed with the first value of the dropdown.

I can get the first number with:

var valor = $(".tt-suggestion p").first().text();

But when trying to set it in the field using:

$("#cidade_estado").val(valor);

or

$("#cidade_estado").val(valor);

Nothing happens.

I know he’s getting into the if since he does the:

$("#telefone").focus();

Normally and displays the values in the console.

$(document).ready(function () {
    $('input.cidade_estado').typeahead({
        name: 'municipio',
        remote: 'action/getMunicipios.php?query=%QUERY'
    }).keypress(function (e) {
         var valor = $(".tt-suggestion p").first().text();
         console.log("Aqui: " + valor);
        if (e.which == 13) {
            console.log("13: " + valor);
            $("#cidade_estado").val(valor);
            $('#cidade_estado').attr('value', valor);
            $("#telefone").focus();
            return false;
        }
    });
});

html:

            <form id="formemail" action="action/enviaemail.php" method="post">
                <input name="nome" type="text" placeholder="Nome completo">
                <input name="empresa" type="text" placeholder="Empresa">
                <input id="cidade_estado" name="cidade_estado" class="cidade_estado" type="text" placeholder="Cidade">
                <input name="telefone" class="grupocontato" id="telefone" type="text" placeholder="Telefone">
                <input name="email" class="grupocontato" type="text" placeholder="Email">
                <textarea name="mensagem" placeholder="Mensagem"></textarea>
                <input class="btnContato" type="submit" value="Enviar">
            </form>

Console:

inserir a descrição da imagem aqui

  • Is the value being printed normally in the console.log? You could also put the HTML snippet?

  • The valos is shown normally in the console, I also tried to put any string there and gives the same effect, ie nothing.

  • Have you tried setting yourself ? $(this). val(value)

  • I didn’t, but I tested it here just now and it didn’t work.

  • What console.log("13: " + valor); display?

  • makes the following test: console.log("13: " + valor, $('#cidade_estado').length);.

  • The result was this: "13: São Francisco do Guaporé, RO 1", that is to say, he is not really doing this.

  • I made the following console.log("13: " + value, $(this).text().length); it returned 25, ie it arrow, but somehow the plugins does not show or remove this value.

  • 1

    Ah, you want to fill in the plugin field ? need to trigger a click on the value in question, see which element identification typeahead renders on the input, and trigere a click on the value you want in view of trying to give a val(value), think so will fill

  • @Anthraxisbr if you want to post the reply so I can mark it as answered.

Show 5 more comments

1 answer

0


With the help of Anthraxisbr the question has been resolved:

$(document).ready(function () {
    $(".twitter-typeahead span").text('dfsd');
    var campo = $('input.cidade_estado');
    campo.keypress(function (e) {
         var valor = $(".tt-suggestion p").first().text();
        if (e.which == 13) {
            $(".tt-suggestion p").trigger( "click" );
            $("#telefone").focus();
            return false;
        }
    }).typeahead({
        name: 'municipio',
        remote: 'action/getMunicipios.php?query=%QUERY'
    });
});

Browser other questions tagged

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