use dynamic INPUT for database query

Asked

Viewed 455 times

0

I have this script in html and jquery searching in the database what the user wrote in the input

<div class="form-group col-md-8" id="dynamicDiv1" >
     <label for="categoria">Categoria</label>
     <input type="search" name="categoria[]" autocomplete="off" class="form-control" id="busca" placeholder="Buscar por categoria">                          
     <div class="col-md-12 rm_padding">
          <ul class="list-group">
          </ul>
     </div>
</div> 

and shows what comes from the database in this section

<div class="col-md-12 rm_padding">
      <ul class="list-group">
      </ul>
</div>

//Busca categoria dinamicamente
$(function () {
    $('#busca').keyup(function () {

        var pesquisa = $(this).val();
        $.post('pesquisa.php', {categoria: pesquisa}, function (r) {
            $('.list-group').html(r);
        });
    });

    $('.list-group').delegate('li', 'click', function () {

        var texto = $(this).text();
        $('#busca').val(texto);
        $('.list-group').html('');

    });

    $('body').click(function (event) {
        if (event.target.id !== 'busca') {
            $('.list-group').html('');
        }
    });
});

My problem is the input in which the user type it is dynamic or a field always appears as required but it can add 3 more fields

script to add fields

var contador = 0;
var limite = 3;

$(function () {
    var scntDiv = $('#dynamicDiv1');

    $(document).on('click', '#addInput1', function () {
        if (contador < limite) {
            $('<div id="dinamic">' +
                    '<input type="search" id="busca" required="" name="categoria[]" class="form-control" placeholder="Adicionar Categoria" /> ' +                     
                    '<div class="col-md-12 rm_padding"><ul class="list-group"></ul></div>'+
                    '<br><a class="btn btn-danger btn-sm left" href="javascript:void(0)" id="remInput">' +
                    '<span class="glyphicon glyphicon-minus " aria-hidden="true"></span> ' +
                    'Remover Categoria' +
                    '</a>' +
                    '</div>').appendTo(scntDiv);
            contador++; // incremento do contador
        }
        return false;
    });

    $(document).on('click', '#remInput', function () {
        $('#dinamic').remove();
        if (contador > 0)
            contador--; // remover do contador tb
        return false;
    });
});

how do I make for dynamic research to also work in those fields that are created dynamically.

1 answer

0


JQUERY DELEGATE

jQuery assigns an event to the item when called, i.e., if when jQuery was executed the item did not yet exist, it would be impossible to attach the function keyup to him.

However, using the delegate, the parent element is the one who will assign the child elements the desired function whenever a new element is created:

$('button').click(function(){
  $('body').append("<p><input></p>");
});

$('body').delegate('input', 'keyup', function(){ // body é o Elemento pai de input
  alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<button>Adicionar</button>
  
  
</body>

In your SEARCH function changes as follows:

//Busca categoria dinamicamente
$(function () {
var buscaAtual = $("#busca");
    $('body').delegate('#busca', 'keyup', function(){

        var pesquisa = $(this).val();
        buscaAtual = $(this);
        $.post('pesquisa.php', {categoria: pesquisa}, function (r) {
            $('.list-group').html(r);
        });
    });

    $('.list-group').delegate('li', 'click', function () {

        var texto = $(this).text();
        buscaAtual.val(texto);
        $('.list-group').html('');

    });

    $('body').click(function (event) {
        if (event.target.id !== 'busca') {
            $('.list-group').html('');
        }
    });
});

SOURCE

  • But in case the return coming from the bank would be applied to all fields?

  • As long as they’re tag kids <body> (what they will be) will apply to all inputs (you can replace the 'input' there with 'form-control' or even '#search', but the idea of having several elements with the same ID is pretty bad)

  • Ai that ta the problem I need that each dynamic field can make its search in the bank

  • What’s the problem? Dynamic search will work on dynamically created items.

  • my problem is that dynamic search does not work in the dynamic fields

  • @Did you understand my answer? Change $('#busca').keyup(function () { for $('body').delegate('#busca', 'keyup', function(){ that your search will work in the dynamic fields

  • Sorry had not understood. Now it worked however when I click on the result it does not fill in the input

  • It is because you have many elements called #search, you would have to do so, add this code: var pesquisa = $(this).val(); &#xA;var buscaAtual = $(this); and replaces here $('#busca').val(texto); for buscaAtual.val(texto);

  • ReferenceError: buscaAtual is not defined[Learn More], This is me returning that error on the console. sorry so many questions is that I’m getting bored eating to learn js

  • I have made Global your search variable and you can try, any doubt we will continue this discussion in chat.

Show 5 more comments

Browser other questions tagged

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