javascript 变量返回为未定义

发布于 2024-12-20 13:55:35 字数 1852 浏览 2 评论 0原文

我目前正在使用一些 javscript,其背后的基本思维过程是,您单击一个链接,该链接将生成并输入到篮子中,我当前在控制台中记录了发送到篮子的内容。当我点击一个链接时,它会触发 additem() 函数,

function additem(desc,price,quantity)
    {

        var x = items.length - 1;
        var found = (x == 0);
        var y = new Item(desc,price,parseInt(quantity));
        console.log(y);
        var z = y;

        while((x != 0) && (found == false))
        {
            z = items[x];

            if(z != null)
            {
                if(z.M_desc == desc)
                {
                    found = true;
                }
                else
                {
                    x--;
                }
            }
            else
            {
                x--;
            }
        }

        if(found == false)
        {
            items[++numitems] = y;
        }
        else
        {
            items[x].M_quantity += quantity;
        }

        updatecart();
    }

我在控制台中看到的是以下内容,

Item { M_desc="Item 2", M_price="10", M_quantity=1}

但是当我点击它时,我得到以下内容,

M_desc
    "Item 2"

M_price
    "10"

M_quantity
    undefined

M_quantity 如何为 1 然后未定义在另一个控制台视图中?

===== 编辑=====

我认为问题源于这里,我的 JS 代码以该行开头。

var items = new Array(2);

现在我假设这会创建一个新数组,因为当我在它之后立即运行 console.log(items) 时,我会得到 [undefined, undefined]

使用以下代码在单击链接时调用 additem,

HTML

<a class='product' rel='"+category[i].product[j].price+"' href=''>"+ category[i].product[j].description + "</a>

JS

$('.product').live("click",function(e){
    additem($(this).text(), $(this).attr('rel'), 1);
e.preventDefault();
});

此时会出现问题,因为 items[x] 始终为 null 或未定义。我不知道为什么。

I am working with some javscript at the moment, the basic thought process behind it is that, you click a link and that links produces and entry in a basket, I currently logged what gets sent to the basket in my console. When I click a link it triggers the additem() function,

function additem(desc,price,quantity)
    {

        var x = items.length - 1;
        var found = (x == 0);
        var y = new Item(desc,price,parseInt(quantity));
        console.log(y);
        var z = y;

        while((x != 0) && (found == false))
        {
            z = items[x];

            if(z != null)
            {
                if(z.M_desc == desc)
                {
                    found = true;
                }
                else
                {
                    x--;
                }
            }
            else
            {
                x--;
            }
        }

        if(found == false)
        {
            items[++numitems] = y;
        }
        else
        {
            items[x].M_quantity += quantity;
        }

        updatecart();
    }

What I see in the console is the following,

Item { M_desc="Item 2", M_price="10", M_quantity=1}

however when I click into it, I get the following ,

M_desc
    "Item 2"

M_price
    "10"

M_quantity
    undefined

How can M_quantity be 1 and then undefined in another console view?

===== EDIT =====

I think the problem stems form here, my JS code starts with the line.

var items = new Array(2);

now I assume that this creates a new array as when I run console.log(items) straight after it I get [undefined, undefined].

additem is called on the click of a link using the following code,

HTML

<a class='product' rel='"+category[i].product[j].price+"' href=''>"+ category[i].product[j].description + "</a>

JS

$('.product').live("click",function(e){
    additem($(this).text(), $(this).attr('rel'), 1);
e.preventDefault();
});

This is when the problems arise as items[x] is either always null or undefined. and I not sure why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福不弃 2024-12-27 13:55:35

您的 updatecart 函数几乎同时被调用。 items[i].M_quantity = $('input[name="qty"]').val() 是有问题的行。输入为空,并将您的数量替换为未定义。

Your updatecart function is being called almost simultaneously. items[i].M_quantity = $('input[name="qty"]').val() is the problematic line. The input is null and is replacing your quantity with undefined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文