gpt4 book ai didi

javascript - 如何在 jQuery 中存储 key

转载 作者:行者123 更新时间:2023-11-28 10:51:52 26 4
gpt4 key购买 nike

这是我的代码:

function addtocart(id,price) {
var qty = $("#txt_qty" + id).val();
var qty1 = new Array();
if (qty == '' || qty == 0) {
alert('Please Enter Quantity');
$("#txt_qty" + id).focus();
return false;
}
if (qty != '' && type != '') {
proid.push(id);
var text = 'qty'+id+'='+qty;
var keyValuePair = text.replace(/ /g,'').split('=');
qty1.push([keyValuePair[0]] = [keyValuePair[1]]);
}

我需要将 key 存储在 qty1 数组中,我已经有名为 qty 的值

如何在 qty1 数组中动态存储 key

我正在尝试上述尝试,但没有成功

谁能帮帮我

最佳答案

如果您已经获得 keyvaluepair[0]keyvaluepair[1] 的数据值,请尝试使用此:

var myObj = {}; // declare obj container
myObj[keyvaluepair[0]] = keyvaluepair[1]; // set key and value based on your variable
qty1.push(obj); // finally push obj into your qty1(array) variable

这只是一个替代方案,您可以尝试使用@Disha V提供的解决方案。

关于javascript - 如何在 jQuery 中存储 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31780745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com