Create a jquery array from the input names table

I hope this hasn’t been answered somewhere :)

I am using the follwing form input field to allow customers to update the quantity of items in their basket:

<input type="text" name="item_qty[]" value="">

So each item in the basket has an input field as above and there is one submit button to update all product quantities in one go.

My problem is that I cannot work out how to create the required array in jquery to send through ajax using the array in the name element (I want to keep it like this if possible as I want to ensure that the form will work as a normal submit to the PHP file if there is any reason why the jquery can’t be initialised).

I have thought that maybe the name should be the key from the basket array but not sure. Can anyone advise of the best way to do this and if it is to keep the input field as it is now, how I get the required data to create an array in jquery please.

So, if I understand you correctly you have some output similar to this?

and you want a result with something like:

[{item_qty[0], 4}, {item_qty[1], 1}, ...]

If this is correct, I’ve thrown something together in this fiddle:

EDIT: For the associative array, you need to create a JS Object, see this fiddle:

Hope this helps

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Create a jquery array from the input names table

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录