Increase variables dynamically [duplicated]

0

I want to increase the variable of each of the fields ( NUMERO_ITEM1 , DESCRIPCION_ITEM1 ) dynamically, that is

DESCRIPCION_ITEM1 , DESCRIPCION_ITEM2 ... DESCRIPCION_ITEMn ...

By means of variable $post I made the query.

$(document).ready(function() {
   $("#resultadoBusqueda").html('Vacio');  });

  function buscar() {

var textoBusqueda = $("#NUMERO_ITEM1").val();

if (textoBusqueda != "") {
  $.post("buscar.php", {valorBusqueda: textoBusqueda}, function(mensaje) {
      $("#DESCRIPCION_ITEM1").html(mensaje);
   }); 
} else { 
  $("#DESCRIPCION_ITEM1").html('Vacio');  };
    
asked by Anderviver 14.06.2017 в 21:53
source

3 answers

1

You should use a variable to make use of the increment, in the following way.

    $(document).ready(function() {
    $("#resultadoBusqueda").html('Vacio');  });        

    function buscar() {

    var autoincremento = 0; <-- INICIAS EL AUTO INCREMENTABLE
    var textoBusqueda = $("#NUMERO_ITEM1").val();

    if (textoBusqueda != "") {

       $.ajax({
         url: "buscar.php",
         type: "POST",
         dataType: "JSON", // Tipo de archivo de respuesta en este caso sera un JSON
         data: {
           // parametro: variable // agrego las variables de consulta si son necesarias
         },
         success: function(data){

           var html = '';
           $.each(data,function(indice,fila){
              html += '<input value="' + fila['campo'] + '">';
           });

           $('#mycampo').html(html);

         }
       });

    } else { 
      $("#DESCRIPCION_ITEM"+(autoincremento++)).html('Vacio');  };
    
answered by 14.06.2017 / 22:04
source
0

Try the following code:

    $(document).ready(function() {
    $("#resultadoBusqueda").html('Vacio');  });

    function buscar() {

    var textoBusqueda = $("#NUMERO_ITEM1").val();

    if (textoBusqueda != "") { 

      $.post( "buscar.php", { valorBusqueda: textoBusqueda })
          .done(function( data ) {
             $("#DESCRIPCION_ITEM1").html(data);
          })
          .fail(function(data) {
            alert( "error" + data );
          });
    } else { 
          $("#DESCRIPCION_ITEM1").html('Vacio');  
    }
   }
    
answered by 14.06.2017 в 22:06
0

I did not have time to implement your case with the example I gave you. You can guide yourself with what I just did.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
var object = {}; //DECLARAS UNA VARIABLE DE TIPO OBJETO, CUANDO SON LLAVES, SERAN OBJETOS
for(var i = 0; i < 10; i++){
	var chain = "NUMERO_ITEM"+i;//A TU VARIABLE CHAIN LE PONES EL NOMBRE MAS DONDE VAYA i
 	object[chain] = 'VALOR '+i;//A TU OBJETO EN SU INDICE CHAIN OSEA OBJETO_0 POR EJEMPLO LE ASIGNAS UN VALOR, EN ESTE CASO VALOR0
}
alert(object.NUMERO_ITEM0);//CAMBIA EL NUMERO, POR EJEMPLO object.NUMERO_ITEM8

</script>

I'll leave you the Fiddle too

  

link

    
answered by 14.06.2017 в 23:58