开发者

Jquery: "G is undefined" error while using string.match

I'm using J开发者_Python百科query 1.3 and this is the code i've isolated the code that throws an error in firebug as follows: "G is undefined"

    var product = $("#id :selected");         // This is a dropdown
    var prodTxt = product.text();             // Returns string as expected
    var price = prodTxt.match(/\$[0-9]{3}/);  // Commenting this out removes the error. 
    //var price = prodTxt.match(/\$[0-9]*/); 

I thought it had something to do with the */ in the regex but still doesn't work.

The regex looks for any price as indicated by a dollar sign.

I suppose there are other workarounds and I may just do that, but I'm still confused as to what I am doing wrong. Thanks for your help.

UPDATE-SOLVED

var price = String(prodTxt.match(/\$[0-9]{3}/));

Not sure why that worked. I'm sure someone much smarter than me could explain how not typecasting it infuriated the ominous "g" variable.


I don't get that error. Just be aware that .match() returns an array, so you may need to access the match by its index [0].

Try it out: http://jsfiddle.net/dJ294/

$("#id").change(function() {
    var product = $("#id :selected");         // This is a dropdown
    var prodTxt = product.text();             // Returns string as expected
    var price = prodTxt.match(/\$[0-9]{3}/);  // Commenting this out removes the error. 
    //var price = prodTxt.match(/\$[0-9]*/);

         // Access match by its index number
    $('body').append('<br/>' + price[0]);
});​


The problem comes after the match line. Most likely, it's caused when you try to use the "price" variable as a string when match returns an array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜