简体   繁体   English

对象在jQuery插件中没有方法'charAt'

[英]Object has no method 'charAt' in jQuery plugin

I am attempting to use the autoNumeric jQuery plug-in which helps with the conversion of various currencies in jQuery. 我正在尝试使用autoNumeric jQuery插件,插件有助于jQuery中各种货币的转换。

The plug-in itself works when I use it in a jsFiddle example . 当我在jsFiddle示例中使用插件时,插件本身即可工作。

$(function () {
    $('.money').autoNumeric('init', {
        aSign: '$',
        vMin: '-999999999.99',
        nBracket: '(,)'
    });
});

However, as soon as I integrate it into a big, legacy project, I start receiving the above error on line 194. I know why I'm getting the error - a string is not being passed into the negativeBracket function ( negativeBracket(s, nBracket, oEvent) is the signature). 但是,一旦将其集成到一个大型的旧项目中,我就会在194行开始收到上述错误。我知道为什么会收到错误-字符串未传递到negativeBracket函数中( negativeBracket(s, nBracket, oEvent)是签名)。 Instead, it seems to be a jQuery object - e.fn.init 1 . 相反,它似乎是一个jQuery对象-e.fn.init 1 I'm confused on how this might be happening. 我对这可能如何发生感到困惑。 I realize the community may not be able to give a direct answer, but I would love (and will accept as an answer) being pointed in the right direction as nothing has jumped out at me so far. 我意识到社区可能无法给出直接答案,但是我很乐意(并将接受为答案)指向正确的方向,因为到目前为止,还没有任何事情出现在我身上。

Update 更新资料

So, have some additional info that may be of help. 因此,有一些其他信息可能会有所帮助。 It still has me stumped how it's happening (unfortunately, the answers below didn't help to provide any additional insight). 它仍然让我感到困惑(不幸的是,下面的回答并没有提供任何其他见解)。 When I link in autoNumeric, I key it off of any text field with the class money. 当我链接autoNumeric时,我将其从任何文本字段的money类中删除。 It does work as I am typing in the box. 当我在框中输入内容时,它确实起作用。 I can see see formatting. 我可以看到格式。 However, when I tab into a new box, the box I just finished typing in clears itself completely after hitting line 152 in autoNumeric with the same exact error. 但是,当我跳进一个新框时,刚敲完的框在autoNumeric中碰到第152行后,会完全清除,并且具有相同的确切错误。

@Carlos487 was correct in his answer when he said I have an object that is not a string. @ Carlos487在回答我说我有一个不是字符串的对象时是正确的。 I instead have an object that, I believe, is a function. 我相信我有一个对象是一个函数。 Here's what I'm seeing in Chrome debugger tools: 这是我在Chrome调试器工具中看到的内容:

e.fn.init[1]

> 0: input#price.money required
> context: input#price.money required
 length: 1
 selector: ""
> __proto__: Object[0]

The "arrowed" items can be further expanded out. “箭头”项可以进一步扩展。 I don't know if this provides any more clues, but it's at least something a bit different. 我不知道这是否能提供更多线索,但至少有些不同。

The errors like 像这样的错误

no method XXXXX in Object 对象中没有方法XXXXX

are produced because you are trying to call obj.XXXX() and obj is not of the desired type, in your particular case a string . 产生的原因是您尝试调用obj.XXXX(),而obj不是所需的类型,在特定情况下为string

Have you tried in another browser because older or IE can be a little troublesome. 您是否在其他浏览器中尝试过,因为较旧的版本或IE可能会有些麻烦。 I would recomend using chrome developer tools with your legacy app to see if anything else is conflicting or producing the error 我建议您在旧版应用中使用chrome开发人员工具,看看是否还有其他冲突或产生错误

I will bet money that you are using a second library which is interfering with jQuery. 我敢打赌,您正在使用第二个干扰jQuery的库。 It has probably overridden $ with its own function. 它可能已经用自己的功能覆盖了$

Try using jQuery instead of $ : 尝试使用jQuery代替$

jQuery(function () {
    jQuery('.money').autoNumeric('init', {
        aSign: '$',
        vMin: '-999999999.99',
        nBracket: '(,)'
    });
});

It turns out that the issue was a myriad of issue compounding into the error I saw. 事实证明,这个问题是无数个问题,并加到我看到的错误中。 A couple things that was happening: 发生了几件事:

  1. The validator plug-in was wrapping the jQuery object in its own structure (hence the charAt issue). 验证程序插件将jQuery对象包装在其自己的结构中(因此出现了charAt问题)。
  2. Once I fixed that, I also learned that some homegrown code was also wiping and rewriting data into the field to provide formatting (which is what autoNumeric is also doing), so autoNumeric would bomb out because it would get a null value and attempt to format it. 修正此问题后,我还了解到一些本地代码也正在擦除数据并将其重写到字段中以提供格式设置(autoNumeric也正在这样做),因此autoNumeric会炸掉,因为它将得到一个空值并尝试格式化它。

There was some other random craziness that also needed cleaned up. 还有一些其他的随机疯狂也需要清理。 So...issue resolved! 所以...问题解决了! Still more to work on, but at least this hurdle is past. 还有更多工作要做,但是至少这个障碍已经过去了。 Thanks all for your help. 感谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM