Valid if a value has already been entered, that does not allow to enter more, jquery?

0

Currently I generate a dynamic list with jquery, the dynamic list is formed by an arrangement of objects which remains this way:

var lista_productos_elegir = $('.lista_productos_elegir');
                    valCarrito = response.data;
                       $.each(valCarrito, function (i) {
                            $('<li/>').addClass('swipeout swipeout_productos1')
                                .append($('<div/>').addClass('swipeout-content item-content')
                                    .append($('<div/>').addClass('item-media')
                                        .append('<img src="' + valCarrito[i].Url + '" onerror="this.onerror=null;" alt="" width="40" height="60" />'))
                                    .append($('<div/>').addClass('item-inner')
                                        .append($('<div/>').addClass('item-title-row')
                                            .append($('<div style="font-size: 12px;"/>').addClass('item-title').append(valCarrito[i].Codigo_Inventario + " - " + valCarrito[i].Descripcion))
                                            .append('<div class="codInventario_material" style="display:none">' + valCarrito[i].Codigo_Inventario + '</div>')
                                            .append('<div class="tipo_material" style="display:none">' + valCarrito[i].Tipo + '</div>')
                                            .append('<div class="tipo_costo_material" style="display:none">' + valCarrito[i].TipoCosto + '</div>')
                                            .append('<div class="preciosocio_material" style="display:none">' + valCarrito[i].PrecioSocio + '</div>')
                                            .append('<div class="volumenNegocio_material" style="display:none">' + valCarrito[i].VolumenNegocio + '</div>')
                                            .append('<div class="cantidadProducto_material" style="display:none">' + valCarrito[i].Cantidad + '</div>')
                                            .append('<div class="item-after" data-unit-price="' + valCarrito[i].PrecioSocio + '"><span class="product-amount" style="font-size: 12px;">' + valCarrito[i].PrecioSocio + '</span></div>')
                                        )
                                        .append($('<div/>').addClass('item-text')
                                            /*.append('<input type="number" class="add_material_impreso" value="0" min="0" placeholder="Ingrese cantidad">')*/
                                              .append($('<div/>').addClass('chip chip-small').append('<div class="chip-label"><span class="product-quantity">' + valCarrito[i].Cantidad + '</span></div>'))
                                              .append($('<div style="background: rgba(0, 0, 0, 0);"/>').addClass('chip chip-small').append('<div class="chip-label"><button class="sumarProducto1">+</button></div>'))
                                              .append($('<div style="background: rgba(0, 0, 0, 0);"/>').addClass('chip chip-small').append('<div class="chip-label"><button class="restarProducto1">-</button></div>')) 
                                        )
                                    )
                                )
                                .appendTo(lista_productos_elegir);

for such situation I will increase the value of the quantity with this function, as you can see Product 1, product 2 and product 3 have quantity 1, that if it is greater than one you should no longer allow:

$$('.lista_productos_elegir').on('click','.sumarProducto1', function(e){
        var list                          = $(this).closest('.swipeout_productos1');
        var el_product_quantity_producto1 = list.find('.product-quantity');
        var tipo_material                 = list.find('.tipo_material').text();
        var tipo_costo_material           = list.find('.tipo_costo_material').text();
        var codinv_material               = list.find('.codInventario_material').text();
        var preciosocio_material          = list.find('.preciosocio_material').text();
        var volumenNegocio_material       = list.find('.volumenNegocio_material').text();
            product_sum                   = parseInt(el_product_quantity_producto1.text());
            cantidad_suma                 = product_sum+1;
            el_product_quantity_producto1.text(cantidad_suma);
            if (cantidad_suma > 1){
                myApp.alert("Puede llevar un máximo de 1 productos");
                product_sum = 1;
                el_product_quantity_producto1.text(product_sum);
            }
    });

The detail is that only in that list I have allowed to increase any of those products to the value quantity 1, that is, if product 1 already has 1, in the other records as Product 1,2,3 and 4 already You should not allow me to increase, until you subtract the Product that is already assigned 1.

Thank you very much in advance.

    
asked by JG_GJ 14.09.2018 в 21:38
source

1 answer

1

I can not comment, but I hope I understand: You have several options I see that the quantity input has a class with name "product-quantity", and this field is generated by each product, so you can get all those input like this:

var cantidadInputs = $(".product-quantity")

Declares a flag to validate if a value already exists in some input that is equal to 1:

var existeValor = false;

This returns all the tags with that class in an array, in this case all the quantity input. now you do a for touring "quantityInputs".

for(var i=0; i<$cantidadInputs.length;i++){
  //haces la lógica validando cada registro
  if(Number($cantidadInputs[i].value)>=1){
    //Cambias a true el flag y rompes el for
    existeValor = true;
    break;
  }
}

Lastly valid if there is a value is equal to true, if so, boots the message that you can not if you do not, do the flow you want.

There are many options, that occurred to me right now. I hope I have helped you.

if(existeValor){
  // mensaje que no se puede aumentar otro item
}else{
  //Permites aumentar el otro item.
}
    
answered by 15.09.2018 / 00:41
source