Javascript 在jquery中不触发事件

Javascript 在jquery中不触发事件,javascript,jquery,json,Javascript,Jquery,Json,我有一个列表,页面加载,加载项目。当单击页面中的复选框时,使用json加载项目 $('.content-items:checkbox').on('change', function () { $.get(newUrl, function (data) { $("#ProductsPartialView").empty(); $("#ProductsPartialView").append(data); $('#loadingModal').mod

我有一个列表,页面加载,加载项目。当单击页面中的复选框时,使用json加载项目

$('.content-items:checkbox').on('change',
  function () {
    $.get(newUrl, function (data) {

      $("#ProductsPartialView").empty();
      $("#ProductsPartialView").append(data);
      $('#loadingModal').modal('hide');

    }).fail(function (xhr, err) {
      alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
      alert("responseText: " + xhr.responseText);
    });
  });
单击项目元素时,运行函数

$('.compare-items:checkbox').on('change',
   function() {
      $('#loadingModal').modal('show');
      var value = $(this).data('id');
      $.getJSON("/store/AddToCompare", {
         id: value, add: true
      }, function (result) {
         $(".compare-footer").show();
         $(".compare-count").html(result);
         $('#loadingModal').modal('hide');
      });
    }
});
在页面加载中加载项时,启动此jquery,但在第一个代码中加载带有json的项时,不要启动第二个函数。

为此,您需要
$(document).on(事件,选择器,cb{})当您要对动态生成的数据执行操作时

$('.compare-items:checkbox').on('change',
   function() {
      $('#loadingModal').modal('show');
      var value = $(this).data('id');
      $.getJSON("/store/AddToCompare", {
         id: value, add: true
      }, function (result) {
         $(".compare-footer").show();
         $(".compare-count").html(result);
         $('#loadingModal').modal('hide');
      });
    }
});
$(document).on('change','.compare-items:checkbox',
   function() {...
为此,您需要
$(document).on(事件,选择器,cb{})当您要对动态生成的数据执行操作时

$(document).on('change','.compare-items:checkbox',
   function() {...