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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15905221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:31:53  来源:igfitidea点击:

Javascript callback function throws error "Callback is not a function" in firefox

javascript

提问by DavidB

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", ".ConfigNameDDL", "GetParameters");
    };
    CascadeDropDowns(".DeviceTypeDDL", ".ConfigGroupDDL", "GetGroups", callback);
  });
  $(".ConfigGroupDDL").change(function () {
    CascadeDropDowns(".ConfigGroupDDL", ".ConfigNameDDL", "GetParameters");
  });
});

This runs fine and cascades the dropdowns in the right order, but firefox debugger shows an error and ie throws an alert and asks if Id liek to debug.

这运行良好并以正确的顺序级联下拉菜单,但 Firefox 调试器显示错误,即抛出警报并询问是否需要调试。

Any advice would be great

任何建议都会很棒

回答by epascarello

It is because you are not always passing the callback into that method.

这是因为您并不总是将回调传递给该方法。

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;
  if(callback) callback();  //check before calling it. 
}

回答by plalx

It's because you are not always providing a callbackto the CascadeDropDownsfunction.

这是因为你并不总是提供callbackCascadeDropDowns功能。

E.g.

例如

CascadeDropDowns(".ConfigGroupDDL", ".ConfigNameDDL", "GetParameters");

CascadeDropDowns(".ConfigGroupDDL", ".ConfigNameDDL", "GetParameters");

You should modify your function to treat the callbackargument as an optionnal argument:

您应该修改您的函数以将callback参数视为可选参数:

if (callback) {
    callback();
}

A common shorthand for that is:

一个常见的简写是:

callback && callback();