javascript如何使用键和复选框值创建多维数组
我需要帮助来创建数组,该数组将通过ajax传递到php页面 这是我的html $document.readyfunction{ $'.p_options'.changefunction{ 如果$this.is:已选中{ $this.next.opts_qty.val1.show; }else$this.next.opts_qty.val0.hide; } }; .操作检查{ 显示:内联块; 保证金:3px0; 文本对齐:左对齐; 宽度:100%; } .opts\U数量{ 宽度:50px; } 巴瑟罗先生, .opts\U数量{ 显示:无; } 函数addtocartid{ arr={}; var误差=0; var=0; 如果$'.p_选项'.length{ $'.p_options'.eachfunction{ 如果$this.is:已选中{ var num=$this.next.opts\u qty.val; 如果num!=&&num>0{ //信息[id]。推送$this.val; arr[id]=$this.val; 选中++; $this.next.opts_数量css'background','fff'; }否则{ $this.next.opts_数量css'background','red'; 错误++; } } }; alertJSON.stringifyarr; } iferror<1{ $.ajax{ 类型:post,, url:./shop/basket.php, 数据:{product:id,'product_options':arr}, 数据类型:“json”, cache:false, 成功:functiondata{ 警惕“成功”; } }; } } 请从选项中选择尺寸 S-16毫米 M-17毫米 L-18毫米javascript如何使用键和复选框值创建多维数组,javascript,jquery,html,arrays,ajax,Javascript,Jquery,Html,Arrays,Ajax,我需要帮助来创建数组,该数组将通过ajax传递到php页面 这是我的html $document.readyfunction{ $'.p_options'.changefunction{ 如果$this.is:已选中{ $this.next.opts_qty.val1.show; }else$this.next.opts_qty.val0.hide; } }; .操作检查{ 显示:内联块; 保证金:3px0; 文本对齐:左对齐; 宽度:100%; } .opts\U数量{ 宽度:50px; }
添加到篮子而不是使用对象来保存值,您可以使用数组,如示例所示 $document.readyfunction{ $'.p_options'.changefunction{ 如果$this.is:已选中{ $this.next.opts_qty.val1.show; }else$this.next.opts_qty.val0.hide; } }; 函数addtocartid{ arr=[]; var误差=0; var=0; 如果$'.p_选项'.length{ $'.p_options'.eachfunction{ 如果$this.is:已选中{ var num=$this.next.opts\u qty.val; 如果num!=&&num>0{ //信息[id]。推送$this.val; var对象={}; 对象[+$this.val]=num; arr.pushobject; 选中++; $this.next.opts_数量css'background','fff'; }否则{ $this.next.opts_数量css'background','red'; 错误++; } } }; alertJSON.stringifyarr; } iferror<1{ $.ajax{ 类型:post,, url:./shop/basket.php, 数据:{product:id,'product_options':arr}, 数据类型:“json”, cache:false, 成功:functiondata{ 警惕“成功”; } }; } } .操作检查{ 显示:内联块; 保证金:3px0; 文本对齐:左对齐; 宽度:100%; } .opts\U数量{ 宽度:50px; } 巴瑟罗先生, .opts\U数量{ 显示:无; } 请从选项中选择尺寸 S-16毫米 M-17毫米 L-18毫米
添加到篮子而不是使用对象来保存值,您可以使用数组,如示例所示 $document.readyfunction{ $'.p_options'.changefunction{ 如果$this.is:已选中{ $this.next.opts_qty.val1.show; }else$this.next.opts_qty.val0.hide; } }; 函数addtocartid{ arr=[]; var误差=0; var=0; 如果$'.p_选项'.length{ $'.p_options'.eachfunction{ 如果$this.is:已选中{ var num=$this.next.opts\u qty.val; 如果num!=&&num>0{ //信息[id]。推送$this.val; var对象={}; 对象[+$this.val]=num; arr.pushobject; 选中++; $this.next.opts_数量css'background','fff'; }否则{ $this.next.opts_数量css'background','red'; 错误++; } } }; alertJSON.stringifyarr; } iferror<1{ $.ajax{ 类型:post,, url:./shop/basket.php, 数据:{product:id,'product_options':arr}, 数据类型:“json”, cache:false, 成功:functiondata{ 警惕“成功”; } }; } } .操作检查{ 显示:内联块; 保证金:3px0; 文本对齐:左对齐; 宽度:100%; } .opts\U数量{ 宽度:50px; } 巴瑟罗先生, .opts\U数量{ 显示:无; } 请从选项中选择尺寸 S-16毫米 M-17毫米 L-18毫米
添加到篮子您可以为多维数组创建列,然后将其字符串化为JSON 工作
您可以为多维数组创建列,然后将其字符串化为JSON 工作
如果您仍然需要一个对象,您可以尝试 $document.readyfunction{ $'.p_options'.changefunction{ 如果$this.is:已选中{ $this.next.opts_qty.val1.show; }else$this.next.opts_qty.val0.hide; } }; .操作检查{ 显示:内联块; 保证金:3px0; 文本对齐:左对齐; 宽度:100%; } .opts\U数量{ 宽度:50px; } 巴瑟罗先生, .opts\U数量{ 显示:无; } 函数addtocartid{ arr={}; var误差=0; var=0; 如果$'.p_选项'.length{ $'.p_options'.eachfunction{ 如果$this.is:已选中{ var num=$this.next.opts\u qty.val; 如果num!=&&num>0{ if!arr[id] arr[id]=[] arr[id]。推送{ 项目:$this.val, 数量:num }; 选中++; $this.next.opts_数量css'background','fff'; }否则{ $this.next.opts_数量css'background','red'; 错误++; } } }; alertJSON.stringifyarr; } 如果错误<1{ $.ajax{ 类型:post,, url:./shop/basket.php, 数据:{ 产品名称:id,, “产品选项”:arr }, 数据类型:“json”, cache:false, 成功:functiondata{ 警惕“成功”; } }; } } 请从选项中选择尺寸 S-16毫米 M-17毫米 L-18毫米
添加到篮子如果您仍然需要一个对象,您可以尝试 $document.readyfunction{ $'.p_options'.changefunction{ 如果$this.is:已选中{ $this.next.opts_qty.val1.show; }else$this.next.opts_qty.val0.hide; } }; .操作检查{ 显示:内联块; 保证金:3px0; 文本对齐:左对齐; 宽度:100%; } .opts\U数量{ 宽度:50px; } 巴瑟罗先生, .opts\U数量{ 显示:无; } 函数addtocartid{ arr={}; var误差=0; var=0; 如果$'.p_选项'.length{ $'.p_options'.eachfunction{ 如果$this.is:已选中{ var num=$this.next.opts\u qty.val; 如果num!=&&num>0{ if!arr[id] arr[id]=[] arr[id]。推送{ 项目:$this.val, 数量:num }; 选中++; $this.next.opts_数量css'background','fff'; }否则{ $this.next.opts_数量css'background','red'; 错误++; } } }; alertJSON.stringifyarr; } 如果错误<1{ $.ajax{ 类型:post,, url:./shop/basket.php, 数据:{ 产品名称:id,, “产品选项”:arr }, 数据类型:“json”, cache:false, 成功:functiondata{ 警惕“成功”; } }; } } 请从选项中选择尺寸 S-16毫米 M-17毫米 L-18毫米
添加到basket通过更好地利用jQuery集合及其方法,您可以避免很多混乱
function addtocart(id) {
var $checked = $('.p_options').filter(':checked');
$checked.next(".opts_qty").css('background', '#fff'));
var $badns = $checked.filter(function() {
return !(+$(this).next(".opts_qty").val()); // anything falsy is bad
}).next(".opts_qty").css('background', 'red');
if($badns.length === 0) {
var product_options = $checked.get().map(function(opt) {
var object = {};
object[opt.value] = $(opt).next(".opts_qty").val(); // from RenzoCC's answer
return object;
});
return $.ajax({ // don't forget to return the jqXHR (promise)
type: 'post',
url: './shop/basket.php',
data: { 'product':id, 'product_options':product_options},
dataType: 'json',
cache: false
});
} else {
return $.Deferred().reject(new Error('no options selected')).promise(); // and return a rejected promise if validation fails
}
}
通过返回一个承诺,addtocart的调用者可以随时了解结果。通过更好地利用jQuery集合及其方法,您可以避免很多混乱
function addtocart(id) {
var $checked = $('.p_options').filter(':checked');
$checked.next(".opts_qty").css('background', '#fff'));
var $badns = $checked.filter(function() {
return !(+$(this).next(".opts_qty").val()); // anything falsy is bad
}).next(".opts_qty").css('background', 'red');
if($badns.length === 0) {
var product_options = $checked.get().map(function(opt) {
var object = {};
object[opt.value] = $(opt).next(".opts_qty").val(); // from RenzoCC's answer
return object;
});
return $.ajax({ // don't forget to return the jqXHR (promise)
type: 'post',
url: './shop/basket.php',
data: { 'product':id, 'product_options':product_options},
dataType: 'json',
cache: false
});
} else {
return $.Deferred().reject(new Error('no options selected')).promise(); // and return a rejected promise if validation fails
}
}
通过返回一个承诺,addtocart的调用者可以随时了解结果。谢谢你,RenzoCC,它是这样工作的,但是我如何才能向这个数组添加数量,所以最终得到例如[s-16 mm:1,M-17mm:2]?我已经更正了脚本,你可以将一个对象推送到数组中以获得这个结果。。。。如果对你有帮助的话,竖起大拇指。非常感谢伦佐克!谢谢你,RenzoCC,它是这样工作的,但是我如何才能向这个数组添加数量,所以最终得到例如[S-16 mm:1,M-17mm:2]?我已经更正了脚本,你可以将一个对象推到数组中以实现这个结果。。。。如果对你有帮助的话,竖起大拇指。非常感谢伦佐克!