Every time I give a button an element with a different id is created

2

In summary I have a function that is as follows:

 function datos3(){
    resp=$("#ref_mi").val();
    if(resp != "" && resp !=null &&typeof resp != undefined){
        jqmSimpleMessage('Agrega cotizacion: '+resp);
            $.ajax({
            url: "rest/orden/item1/"+resp,
            cache: false,
            success: function(data) {

                $.each(data, function (index, value) {

                   $("#matricula1").append('<a class="ui-btn ui-li-static ui-body-inherit ui-first-child ui-last-child" id="ref1" name="id_item" value="'+value.id_item+'" onclick="limpiar1()">'+value.id_marca+'-'+value.id_item+'-'+value.nomItem+'-'+value.nomMarca+'</a> <a  id="lo1" value="'+value.id_marca+'" name="id_marca" style="display:none;">'+value.id_marca+'</a>');

                });

            },
            error: function(error){
                {
                    if(error.status==401){
                        desAuth();
                    }
                    jqmSimpleMessage("Error en listar infor empleado: "+error.responseText);
                }
            },
            beforeSend: function(xhr, settings) { xhr.setRequestHeader('Authorization','Bearer ' + getVCookie(getVCookie("userPro"))); }
        });
    }else{
        jqmSimpleMessage('Error escoje un Producto');
    }
}

This function does collect the data that I consult based on an id. Then I printed it on a div called a registration1 in the form of a button, my concern is that whenever I create a button on that div (Registration1) I want the ids of each button to be different and I do not know very well how to do it

For example, if that button has Id="refmi" that the next one that is created is Id="refmi1"

    
asked by francisco ayala 22.08.2018 в 20:06
source

3 answers

0

I do not know if it's what you're looking for, but you can do this:

function datos3(i){
resp=$("#ref_mi"+i).val();
//codigo 
} 

But the best thing seems to be to use classes instead of id's.

    
answered by 22.08.2018 в 20:09
0

Declares a variable outside the scope of the function so that its life is until the page is closed or recharged

<script type="text/javascript">


  var nuevoId=0;
//se inicia en 0 para que al primero tenga algun valor
function datos3(){
resp=$("#ref_mi").val();
if(resp != "" && resp !=null &&typeof resp != undefined){
    jqmSimpleMessage('Agrega cotizacion: '+resp);
        $.ajax({
        url: "rest/orden/item1/"+resp,
        cache: false,
        success: function(data) {

            $.each(data, function (index, value) {

              var elemento = "";
                  //creamos el primer boton con su id cambiante
              elemento += '<a class="ui-btn ui-li-static ui-body-inherit ui-first-child ui-last-child" id="ref'+nuevoId+'" name="id_item" value="'+value.id_item+'" onclick="limpiar('+nuevoId+')">'+value.id_marca+'-'+value.id_item+'-'+value.nomItem+'-'+value.nomMarca+'</a>'
                          //creamos el segundo boton
                          //aqui tambien le ponemos un id cambiante para que se pa cual debe eliminar
              elemento += '<a  id="lo'+nuevoId+'" value="'+value.id_marca+'" name="id_marca" style="display:none;">'+value.id_marca+'</a>'

               $("#matricula1").append(elemento);
            //antes de terminar incrementas el valor de la variable
            //en el primer caso el id sera "ref0" y en segundo "ref1"


            nuevoId = nuevoId + 1;
            });

        },
        error: function(error){
            {
                if(error.status==401){
                    desAuth();
                }
                jqmSimpleMessage("Error en listar infor empleado: "+error.responseText);
            }
        },
        beforeSend: function(xhr, settings) { xhr.setRequestHeader('Authorization','Bearer ' + getVCookie(getVCookie("userPro"))); }
    });
}else{
    jqmSimpleMessage('Error escoje un Producto');
}

function limpiar(valor){ 
  //conatenamos el nombre base con el id cambiante
  $("#ref"+valor).remove(); 
  $("#lo"+valor).remove(); 
}
</script>

This if you do not want to count on the values of BD and that you are indendent to capture later, since the values of the BD can be very changing

Update Modify the code form a bit to make it easier to maintain and solve errors by putting the elements separately. (the logic remains the same)

I hope you serve bro, you tell me

    
answered by 22.08.2018 в 20:20
0

After asking my boss xd

function datos3(){
resp=$("#ref_mi").val().split("-");
cant=$("#canti").val();
precio=$("#precio").val();
buscado= $("#item-"+resp[0]*1+"-"+resp[1]*1).val();


if(buscado != "" ||  buscado !=null || buscado != undefined ){

    if(resp != "" && resp !=null &&typeof resp != undefined){

        jqmSimpleMessage('Agrega producto: '+resp[0]*1+"-"+resp[1]*1);

        $("#matricula1").append('<a class="ui-btn ui-li-static ui-body-inherit ui-first-child ui-last-child" id="item-'+resp[0]*1+"-"+resp[1]*1+'" name="id_item" value="'+resp[0]*1+"-"+resp[1]*1+"-"+cant+"-"+precio+'" onclick="limpiar1()">'+resp[2]+"x"+cant+" $"+precio+'</a> '); 
    }else{
        jqmSimpleMessage('Error en la seleccion del producto');
    }

}else{
    jqmSimpleMessage('Error Ya existe est producto');
}

}

function limpiar1(){
$("#item-"+resp[0]*1+"-"+resp[1]*1).remove();

}

    
answered by 22.08.2018 в 22:30