【问题标题】:Uncaught TypeError: Object function () has no method 'replace'未捕获的类型错误:对象函数 () 没有“替换”方法
【发布时间】:2014-02-22 19:22:33
【问题描述】:

我正在使用来自 this site/script 的 jQuery 链接选择下拉框。我把它放在侧边栏中,它在主页上工作正常,但在帖子页面上不起作用,调试器指出了这个错误。

Uncaught TypeError: Object function ()
{for(var a=[];this.length;)a.push(this.splice(Math.random()*this.length,1));
for(;a.length;)this.push(a.pop());return this} has no method 'replace'

它说escapeQuotes : function(str) {return str.replace(/([""\\])/g, "\\$1");有一个错误

脚本的开头:

(function($) {
  $.dynamicDropdown = {
    /**
     * Escape quotation marks and slashes
     * @param {String} String to format
     * @return {String}
     */
    escapeQuotes : function(str) {
      return str.replace(/([""\\])/g, "\\$1");
    },

这是我调用函数的方式。我正在使用 json 文件将选项文本和值拉入选定的框中:

$(document).ready(function(){

    $.getJSON('suggest.json', function(data){

        var $select = $('#mySelectID');

        $.each(data, function (index, o) {
            var $option = $("<option/>").attr("value", o.Box1ID + ":" + o.Box3).text(o.Box1 + "|" + o.Box2 + "|" + o.Box3);
            $select.append($option);
        });

        $("#mySelectID").dynamicDropdown({"delimiter":"|"});

    });
});

编辑:

似乎与我刚刚在网站上放置的随机图像旋转器有冲突。我暂时移除了旋转器,链式选择框工作正常。这是一个example 来显示错误。而this 没有随机旋转器。

Array.prototype.shuffle = function() {
                var s = [];
                while (this.length) s.push(this.splice(Math.random() * this.length, 1));
                while (s.length) this.push(s.pop());
                return this;
            }

            var picData = [
                ['img1','url_1'],
                ['img2','url_2'],
                ['img3','url_3'],

            picO = new Array();
            randIndex = new Array();  //array of random indexes
            for(i=0; i < picData.length; i++){
                picO[i] = new Image();
                picO[i].src = picData[i][0];
                picO[i].alt = picData[i][1];
                randIndex.push(i);
            }
            randIndex.shuffle();
            window.onload=function(){
                var mainImgs = document.getElementById('carouselh').getElementsByTagName('img');


                for(i=0; i < mainImgs.length; i++){
                    mainImgs[i].src = picO[randIndex[i]].src; //assign a random image
                    mainImgs[i].parentNode.href = picData[randIndex[i]][1];
                    mainImgs[i].alt = picData[randIndex[i]][1];
                }

            }

【问题讨论】:

  • str 是数组、对象、null 或 false。
  • 一个错误的值被传递给escapeQuotesstr 是一个函数而不是一个字符串。确保你正确调用了函数,即传递一个字符串(因为你没有显示函数是如何被调用的,这就是所有要说的)。
  • @FelixKling 谢谢。我刚刚添加了其他详细信息。
  • 你能放个小提琴吗? jsfiddle.com
  • 您发布的代码似乎不是问题的原因。这个函数function () {for(var a=[];this.length;)a.push(this.splice(Math.random()*this.length,1)); for(;a.length;)this.push(a.pop());return this} 定义/来自哪里?这就是问题所在。

标签: javascript jquery drop-down-menu jquery-selectbox


【解决方案1】:

在您使用的this script 中,问题很可能出在以下代码行中:

for (var i in parts) {
      name += "[\"" + $.dynamicDropdown.escapeQuotes(parts[i]) + "\"]";
  ...
}

重点是,不要使用for in 循环遍历数组,因为Array.prototype 中可能添加了一个函数,该函数会显示在数组的for in 循环中,只需将其更改为:

for (var i=0;i<parts.length;i++) {
      name += "[\"" + $.dynamicDropdown.escapeQuotes(parts[i]) + "\"]";
  ...
}

那么这将不再具有该功能。


正如您在帖子中添加的那样,原因正是我所指出的。但是如果你仍然坚持使用for in循环,你应该像这样检查parts[i]的类型:

for (var i in parts) {
      if(typeof parts[i] != "string") continue;
      name += "[\"" + $.dynamicDropdown.escapeQuotes(parts[i]) + "\"]";
  ...
}

你对另一个for in循环也有同样的问题:

for (var i in options) {
    option = $(document.createElement("option"))
      .val($.isArray(options[i]) ? i : options[i])
      .html(i)
      .appendTo(select);
}

将其更改为 for (var i=0;i&lt;options.length;i++) 或添加:

if(typeof options[i] != "string") continue;

到你的 for 循环的第一行。

【讨论】:

  • 谢谢。它正在工作。我听从了您的建议,但修改似乎在所有下拉框中添加了一个附加选项“随机播放”。 (example) 有没有可能摆脱它?
  • @user35295:关于这个问题,我更新了我的答案,看看吧。
  • 很抱歉再次打扰您,但是当我添加任一方法时,所选框已损坏并发生错误。第一个说Uncaught ReferenceError: parts is not defined。看起来settingparts 是在选项的for in loop 之后定义的。第二种方法返回Uncaught SyntaxError: Illegal continue statement。是这样吗
  • @user35295:我想你已经使用了for(var i=0;i&lt;parts.length;i++),这是我的错误,如果你看到我已将其更改为for (var i=0;i&lt;options.length;i++)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
  • 2013-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多