Javascript 无法使用jquery清除数组

Javascript 无法使用jquery清除数组,javascript,jquery,Javascript,Jquery,我试图使用array=[]删除数组的所有元素当div值变为“全部清除”,但数组无法清除时。 我在这里所做的就是将选中的复选框值包含在名为“filtervalue”的div中 这是剧本 <script type="text/javascript"> $('.filtercheck, #location, #sorting , #filtervalue').on('click', function(e) { var parameter = []; var

我试图使用
array=[]删除数组的所有元素当div值变为“全部清除”,但数组无法清除时。
我在这里所做的就是将选中的复选框值包含在名为“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>

$('.filtercheck,#位置,#排序,#filtervalue')。on('click',函数(e){
var参数=[];
var过滤器=[];
var指数=”;
var HTML=“”;
var newHTML=“”;
$(“.filtercheck”).each(函数(){
如果(选中此项){
filter.push($(this.val())
parameter.push(this.name+“=”+$(this.val());
}
});
if($(“#位置”).val()){
参数.push($(“#位置”).val();
}
if($('#排序').val()){
参数.push($('#sorting').val());
}
对于(变量i=0;idiv')。每个(函数(){
$(此)。在(“单击”,函数()上){
text=($(this.text());
如果(文本==“全部清除”){
过滤器=[];
console.log(filter.length)
参数=[];
console.log(parameter.length)
$('filtervalue').hide();
}否则{
filter=jQuery.grep(过滤器,函数(n){
返回n!=文本;
});
console.log(过滤器)
console.log(参数)
复选框=$(“:复选框[value=“+text+”]))
if(checkbox.prop(“checked”,true)){
复选框.prop(“选中”,false);
$(this.remove();
}
}
});
});
parameter=parameter.join('&');
$(“#加载图像”).show();
console.log(过滤器)
console.log(参数)
$.ajax({
url:“{instance.get_absolute_url}}”,
键入:“GET”,
数据:参数,
成功:功能(数据){
var a=$(data.find('.prodthumb');
$('.productgrid').html(a);
$(“img.lazy”).lazyload();
},
错误:函数(e){
控制台日志(e.message);
},
完成:函数(){
$(“#加载图像”).hide();
}
});
});
如何删除阵列的所有元素?
谢谢

请用下面的代码检查。只需在顶部移动变量声明,如下所示

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();
        }
      });

    });
var参数=[];
var过滤器=[];
var指数=”;
var HTML=“”;
var newHTML=“”;
$('.filtercheck,#location,#sorting,#filtervalue')。on('click',函数(e){
$(“.filtercheck”).each(函数(){
如果(选中此项){
filter.push($(this.val())
parameter.push(this.name+“=”+$(this.val());
}
});
if($(“#位置”).val()){
参数.push($(“#位置”).val();
}
if($('#排序').val()){
参数.push($('#sorting').val());
}
对于(变量i=0;idiv')。每个(函数(){
$(此)。在(“单击”,函数()上){
text=($(this.text());
如果(文本==“全部清除”){
过滤器=[];
console.log(filter.length)
参数=[];
console.log(parameter.length)
$('filtervalue').hide();
}否则{
filter=jQuery.grep(过滤器,函数(n){
返回n!=文本;
});
console.log(过滤器)
console.log(参数)
复选框=$(“:复选框[value=“+text+”]))
if(checkbox.prop(“checked”,true)){
复选框.prop(“选中”,false);
$(this.remove();
}
}
});
});
parameter=parameter.join('&');
$(“#加载图像”).show();
console.log(过滤器)
console.log(参数)
$.ajax({
url:“{instance.get_absolute_url}}”,
键入:“GET”,
数据:参数,
成功:功能(数据){
var a=$(data.find('.prodthumb');
$('.productgrid').html(a);
$(“img.lazy”).lazyload();
},
错误:函数(e){
控制台日志(e.message);
},
完成:函数(){
$(“#加载图像”).hide();
}
});
});

如何删除数组的所有元素?
如果数组被称为
array
,您只需编写
array=[]
,甚至
array=null
(但如果您想在数组上使用数组函数,则需要将其重新设置为数组)的可能副本我感觉这是一个逻辑错误,因为您似乎将其设置为
过滤器=[]
。太多的代码让我无法在一大早完成此操作。如果filter=[]不起作用,则可能它没有进入if循环。你检查过文本值是否真的是“全部清除”,它是否进入了if循环?我可以进入循环没有效果,它的行为仍然相同