开发者

Help with finding and then replacing text on click function

I am trying to integrate a Shopping Cart ( simplecart-js) into a page using Yoxview lightbox gallery. The lightbox allows for buttons integrated into the title bar and i am trying to get a cart button working which will, when clicked, read specific values within 's and then with a second click function, utilize these values in simplecart ( simplecart.add). Here is the code:

 $('.simpleCart_shelfItem').click(function () {
var name = $(this).find('span').text().replace('$', '');
var price = $(this).find('span').text().replace('$', '');
var thumbs = $(this).find('span').text().replace('$', '');

});                     
var yoxviewCartButton = $("<a>", {
 title: "Add to cart",
 href: "javascript:;",
 onClick: "simpleCart.add('name=' + name, 'price=' + price, 'quantity=1', 'thumb=' + thumbs);"       
    });
    yoxviewCartButton.append($("<img>", {
        src: "res/js/yoxview/images/yoxview_cart_icon.png",
        alt: "Add to cart",
        css: { width: 18, height: 18 }
    }));    

The HTML is as follows:

<a class="item simpleCart_shelfItem" href="slides/foo.jpg" title="Test">
    <span class="cap开发者_如何学JAVAtion item_name">Title 0</span>
    <span class="item_price">$14.99</span>
    <span class="item_thumb">thumbs/foo.jpg</span></a>

I keep getting the error "price is not defined". Obvioulsy I am missing something as far as reading the values in the span. Any help or direction would be appreciated.


Here's the simple answer: scope. Look at this as it currently is:

$('.simpleCart_shelfItem').click(
    function () {
        var name = $(this).find('span').text().replace('$', '');
        var price = $(this).find('span').text().replace('$', '');
        var thumbs = $(this).find('span').text().replace('$', '');
    }
);

var yoxviewCartButton = $("<a>",
    {
        title: "Add to cart",
        href: "javascript:;",
        onClick: "simpleCart.add('name=' + name, 'price=' + price, 'quantity=1', 'thumb=' + thumbs);"
    }
);

yoxviewCartButton.append(
    $("<img>",
        {
            src: "res/js/yoxview/images/yoxview_cart_icon.png",
            alt: "Add to cart",
            css: { width: 18, height: 18 }
        }
    )
);

Do you really expect yoxviewCartButton to work?


You're creating a locally scoped variable price inside your anonymous function passed to the click function. So the price variable is not available outside your anonymous function.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜