javascript - 重置JavaScript中的函数或变量以清除之前的ajax结果

标签 javascript php jquery ajax

我之前遇到过一个问题,也发布在这里。还有一个问题出现了。看来我的 ajax javascript 的先前结果仍然存在,我需要不显示它。我尝试了变量的删除、未定义和空,但它仍然显示我之前查询的结果。

这是代码

请注意,一些括号可能会丢失,因为我只复制了我的实际代码的一部分。

$("#telcooptions").change(function () {
$('#telcofilter').val($("#telcooptions").val());
var telco  = $("#telcooptions").val();
var sdate  = $("#datepicker").val();
var edate  = $("#datepicker1").val();
var region = $("#region").val();
alert(region);
  $.ajax({
  url: "ajax/month.php",
  dataType: "json", 
  type: 'POST',
  data: { telcos: telco, start: sdate, end: edate, regions: region},

  success: function (dataofconfirm) {
    month = dataofconfirm;
    //-----------------------
    //-START OF PRICE AJAX
    //-----------------------
    $.ajax({
      url: "ajax/prices.php",
      dataType: "json",
      type: 'POST',
      data: { telcos: telco, start: sdate, end: edate, regions: region},

      success: function (dataofconfirm) {
        price = dataofconfirm;
        //-----------------------
        //- MONTHLY SALES CHART -
        //-----------------------

        months = month;
        prices = price;

        alert(months);
        alert(prices);
        // Get context with jQuery - using jQuery's .get() method.
        var salesChartCanvas = $("#salesChart").get(0).getContext("2d");
        // This will get the first returned node in the jQuery collection.
        var salesChart = new Chart(salesChartCanvas);

        var salesChartData = {
          labels: months,
          datasets: [
            {
              label: "XXXXX",
              fillColor: "rgb(255,40,40)",
              strokeColor: "rgb(255,40,40)",
              pointColor: "rgb(253,16,16)",
              pointStrokeColor: "#c1c7d1",
              pointHighlightFill: "#fff",
              pointHighlightStroke: "rgb(220,220,220)",
              data: price
            }
          ]
        };

        var salesChartOptions = {
          //Boolean - If we should show the scale at all
          showScale: true,
          //Boolean - Whether grid lines are shown across the chart
          scaleShowGridLines: false,
          //String - Colour of the grid lines
          scaleGridLineColor: "rgba(0,0,0,.05)",
          //Number - Width of the grid lines
          scaleGridLineWidth: 1,
          //Boolean - Whether to show horizontal lines (except X axis)
          scaleShowHorizontalLines: true,
          //Boolean - Whether to show vertical lines (except Y axis)
          scaleShowVerticalLines: true,
          //Boolean - Whether the line is curved between points
          bezierCurve: true,
          //Number - Tension of the bezier curve between points
          bezierCurveTension: 0.3,
          //Boolean - Whether to show a dot for each point
          pointDot: true,
          //Number - Radius of each point dot in pixels
          pointDotRadius: 4,
          //Number - Pixel width of point dot stroke
          pointDotStrokeWidth: 1,
          //Number - amount extra to add to the radius to cater for hit detection outside the drawn point
          pointHitDetectionRadius: 20,
          //Boolean - Whether to show a stroke for datasets
          datasetStroke: false,
          //Number - Pixel width of dataset stroke
          datasetStrokeWidth: 2,
          //Boolean - Whether to fill the dataset with a color
          datasetFill: true,
          //String - A legend template
          legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].lineColor%>\"></span><%=datasets[i].label%></li><%}%></ul>",
          //Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
          maintainAspectRatio: false,
          //Boolean - whether to make the chart responsive to window resizing
          responsive: true
        };

        //Create the line chart
        salesChart.Line(salesChartData, salesChartOptions);
        //---------------------------
        //- END XXXXXXCHART -
        //---------------------------
       }
    });
    //-----------------------
    //-END OF XXXXX AJAX
    //-----------------------
   }
});

});

如何重置变量甚至整个函数本身。

最佳答案

好的,我做到了。抱歉给各位带来麻烦了。我所做的是这个

$('#salesChart').remove();
        $('.chart').append('<canvas id="salesChart" style="height: 180px;"></canvas>');

谢谢大家。发布此内容也是为了帮助其他人。

关于javascript - 重置JavaScript中的函数或变量以清除之前的ajax结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40901801/

相关文章:

javascript - 即使定义了函数后,onclick 属性也不起作用

php - 从数据库存储和渲染 Laravel Blade 模板

PHP 使用键创建值数组

jQuery UI 可排序 - 当手动将项目从一个列表移动到另一个列表时触发可排序事件

javascript - 将 "hoverIntent"添加到 .live 函数

javascript - typescript 将此绑定(bind)到数组排序

javascript - 从knockout js中的attr调用模型函数来获取动态属性

javascript - 如何从命令行与社交网站交互(自动 YouTube 发布、查找新视频的标题等)

php - 从 MySQL 数据库中的最后 5 条记录中获取特定列

javascript - 使用 JQuery 分离和恢复来过滤选择列表