无法使用 jquery 清除数组

Unable to clear the array using jquery

本文关键字:jquery 清除数组      更新时间:2023-09-26

当div 值变为"全部清除"但数组无法清除时,我正在尝试使用 array = []; 删除数组的所有元素。我在这里所做的只是在名为"filtervalue"的div 中包含选定的复选框值。

这是脚本

<script type="text/javascript">
    $('.filtercheck, #location, #sorting , #filtervalue').on('click', function(e) {
      var parameter = [];
      var filter = [];
      var index = "";
      var HTML = "";
      var newHTML = "";
      $(".filtercheck").each(function() {
        if (this.checked) {
          filter.push($(this).val())
          parameter.push(this.name + "=" + $(this).val());
        }
      });
      if ($("#location").val()) {
        parameter.push($("#location").val());
      }
      if ($('#sorting').val()) {
        parameter.push($('#sorting').val());
      }

      for (var i = 0; i < filter.length; i++) {
        HTML = HTML + '<div>' + filter[i] + '</div>';
      }
      newHTML = HTML + '<div>' + 'Clear All' + '</div>'
      $('#filtervalue').html(newHTML).show();
      if (filter.length == 0) {
        $('#filtervalue').hide();
      }

      $('div#filtervalue>div').each(function() {
        $(this).on("click", function() {
          text = ($(this).text());
          if (text == 'Clear All') {
            filter = [];
            console.log(filter.length)
            parameter = [];
            console.log(parameter.length)
            $('#filtervalue').hide();
          } else {
            filter = jQuery.grep(filter, function(n) {
              return n != text;
            });
            console.log(filter)
            console.log(parameter)
            checkbox = $(":checkbox[value=" + text + "]")
            if (checkbox.prop("checked", true)) {
              checkbox.prop("checked", false);
              $(this).remove();
            }
          }
        });
      });
      parameter = parameter.join('&');
      $('#loading-image').show();
      console.log(filter)
      console.log(parameter)
      $.ajax({
        url: '{{ instance.get_absolute_url }}',
        type: 'GET',
        data: parameter,
        success: function(data) {
          var a = $(data).find('.prodthumb');
          $('.productgrid').html(a);
          $("img.lazy").lazyload();
        },
        error: function(e) {
          console.log(e.message);
        },
        complete: function() {
          $('#loading-image').hide();
        }
      });
    });
</script>

如何删除数组的所有元素?谢谢

请检查以下代码。 只需将变量声明移到顶部,如下所示。

var parameter = [];
    var filter = [];
    var index = "";
    var HTML = "";
    var newHTML = "";
    $('.filtercheck, #location, #sorting , #filtervalue').on('click', function(e) {   
      $(".filtercheck").each(function() {
        if (this.checked) {
          filter.push($(this).val())
          parameter.push(this.name + "=" + $(this).val());
        }
      });
      if ($("#location").val()) {
        parameter.push($("#location").val());
      }
      if ($('#sorting').val()) {
        parameter.push($('#sorting').val());
      }

      for (var i = 0; i < filter.length; i++) {
        HTML = HTML + '<div>' + filter[i] + '</div>';
      }
      newHTML = HTML + '<div>' + 'Clear All' + '</div>'
      $('#filtervalue').html(newHTML).show();
      if (filter.length == 0) {
        $('#filtervalue').hide();
      }

      $('div#filtervalue>div').each(function() {
        $(this).on("click", function() {
          text = ($(this).text());
          if (text == 'Clear All') {
            filter = [];
            console.log(filter.length)
            parameter = [];
            console.log(parameter.length)
            $('#filtervalue').hide();
          } else {
            filter = jQuery.grep(filter, function(n) {
              return n != text;
            });
            console.log(filter)
            console.log(parameter)
            checkbox = $(":checkbox[value=" + text + "]")
            if (checkbox.prop("checked", true)) {
              checkbox.prop("checked", false);
              $(this).remove();
            }
          }
        });
      });
      parameter = parameter.join('&');
      $('#loading-image').show();
      console.log(filter)
      console.log(parameter)
      $.ajax({
        url: '{{ instance.get_absolute_url }}',
        type: 'GET',
        data: parameter,
        success: function(data) {
          var a = $(data).find('.prodthumb');
          $('.productgrid').html(a);
          $("img.lazy").lazyload();
        },
        error: function(e) {
          console.log(e.message);
        },
        complete: function() {
          $('#loading-image').hide();
        }
      });
    });