Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
Array didn't update after array filter

After i click the remove image if the array have 7 item one remove and i click another it remove another one item but array still 6 it didn't update the last one i delete how can i solve it thanks you for ur time

Jquery

$(document).on('click', '.remove-image', function() {
        var id = $(this).attr('id');
        var product_id =$(this).attr('product_id');
        var name =$(this).attr('name');
        var array=$(this).attr('array');
        var arr = JSON.parse(array);
        // confirm("Are You sure want to delete !");
        var arr = arr.filter(val => val !== name);
        console.log(arr);
        var formData = {
            id: id,
            product_id : product_id,
            image: arr
        };

        // $.ajax({
        //     type: "POST",
        //     url: "{{ url('image/delete') }}",
        //     data: formData,
        //     success: function () {
        //         alert('workinggggg');
        //     },
        //     error: function () {
        //         alert('errorrrrrrrr');
        //     }
        // });

        $(this).parent('div').remove();
    });

Laravel Blade

  @foreach (json_decode($gallery->image) as $key => $image)
                    <div>
                        <span name="{{ $image }}" id="{{ $gallery->id }}" product_id="{{ $product->id }}" array="{{ $gallery->image }}"
                            class="flex text-red-400 pl-3 image cursor-pointer remove-image">X</span>
                        <img src="{{ asset('storage/' . $image) }}" class="pr-5" alt="{{$product->slug}}">
                    </div>
                    {{ var_export($key) }}
                    @endforeach

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your code is correctly gets a filtered array, but it doesn't write the result back to the attribute, and so the next time that code executes, the array is built again from the attribute's value, which still has the original JSON.

So add one statement to this part:

    var array = $(this).attr('array');
    var arr = JSON.parse(array);
    var arr = arr.filter(val => val !== name);
    $(this).attr('array', JSON.stringify(arr));   // <--- 
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda