Javascript回调函数在Firefox中抛出错误“回调不是函数”

前端之家收集整理的这篇文章主要介绍了Javascript回调函数在Firefox中抛出错误“回调不是函数”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
function CascadeDropDowns(parentClass,childClass,action,callback) {
  var DropDownId = $(parentClass + " option:selected").val();

  $.ajax({
    url: "/site/" + action,data: { DropDownId: DropDownId },dataType: "json",type: "POST",error: function () {
      alert("An error occurred.");
    },success: function (data) {
      var items = "";
      $.each(data,function (i,item) {
        items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
      });
      $(childClass).html(items);
      $(childClass)[0].selectedIndex = 0;
      callback();
    }
  });
}

$(document).ready(function () {
  // Populates all child drop downs on load
  var callback = function () {
    CascadeDropDowns(".ConfigGroupDDL",".ConfigNameDDL","GetParameters");
  };

  CascadeDropDowns(".DeviceTypeDDL",".ConfigGroupDDL","GetGroups",callback);

  // Populates all child drop downs parent change
  $(".DeviceTypeDDL").change(function () {
    var callback = function () {
      CascadeDropDowns(".ConfigGroupDDL","GetParameters");
    };
    CascadeDropDowns(".DeviceTypeDDL",callback);
  });
  $(".ConfigGroupDDL").change(function () {
    CascadeDropDowns(".ConfigGroupDDL","GetParameters");
  });
});

这样运行正常,并以正确的顺序级联下拉列表,但是firefox调试器显示错误,即抛出一个警告,并询问Id liek是否进行调试.

任何建议都会很棒

解决方法

这是因为您并不总是将回调传递给该方法.
success: function (data) {
  var items = "";
  $.each(data,item) {
    items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
  });
  $(childClass).html(items);
  $(childClass)[0].selectedIndex = 0;
  if(callback) callback();  //check before calling it. 
}

猜你在找的JavaScript相关文章