最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Creating a keypair object using jQuery and some inputs - Stack Overflow

programmeradmin3浏览0评论

I have a cart on my website and I need to let users easily change the quantity of items they have in their cart at a moment.

Here is the javascript code I have so far:

<script type="text/javascript" language="javascript">
    $(document).ready(function () {

        var items = [];

        $(".item").each(function () {
            var productKey = $(this).find("input[type='hidden']").val();
            var productQuantity = $(this).find("input[type='text']").val();
            items.addKey(productKey, productQuantity); ?
        });

        // 1. Grab the values of each ammount input and it's productId.

        // 2. Send this dictionary of key pairs to a JSON action method.

        // 3. If results are OK, reload this page.
    });
</script>

The ments I wrote are just guidelines for me on how to proceed.

Is there a way to add a key/pair element to an array of sorts? I just need it to have a key and value. Nothing fancy.

I wrote in an addKey() method just for illustrative purposes to show what I want to acplish.

I have a cart on my website and I need to let users easily change the quantity of items they have in their cart at a moment.

Here is the javascript code I have so far:

<script type="text/javascript" language="javascript">
    $(document).ready(function () {

        var items = [];

        $(".item").each(function () {
            var productKey = $(this).find("input[type='hidden']").val();
            var productQuantity = $(this).find("input[type='text']").val();
            items.addKey(productKey, productQuantity); ?
        });

        // 1. Grab the values of each ammount input and it's productId.

        // 2. Send this dictionary of key pairs to a JSON action method.

        // 3. If results are OK, reload this page.
    });
</script>

The ments I wrote are just guidelines for me on how to proceed.

Is there a way to add a key/pair element to an array of sorts? I just need it to have a key and value. Nothing fancy.

I wrote in an addKey() method just for illustrative purposes to show what I want to acplish.

Share Improve this question asked Apr 6, 2012 at 16:10 Only Bolivian HereOnly Bolivian Here 36.8k65 gold badges167 silver badges257 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4
items[productKey] = productQuantity;

In JavaScript, Arrays are Objects (typeof(new Array)==='object'), and Objects can have properties which can be get/set using dot- or bracket- syntax:

var a = [1,2,3];
JSON.stringify(a); // => "[1,2,3]"
a.foo = 'Foo';
a.foo; // => 'Foo'
a['foo']; // => 'Foo'
JSON.stringify(a); // => "[1,2,3]"

So in your case, you can simply the productQuantity value to the productKey attribute of the item array as such:

items[productKey] = productQuantity;
items[productKey]; // => productQuantity

You can add anonymous objects to the items array like:

items.push({
    key: productKey,
    quantity: productQuantity
});

Then access them later as items[0].key or items[0].quantity.

Also you can use JQuery.data method and like that you can also get rid of those hidden.

发布评论

评论列表(0)

  1. 暂无评论