我想发布到'添加',它还包含尺寸,数量,颜色和价格的属性,而无需刷新页面。我怎么能这样做?
使用example.php
// Add item
if (isset($_POST['add'])) {
foreach ($products as $product) {
if ($_POST['id'] == $product->id) {
break;
}
}
$cart->add($product->id, $_POST['qty'], [
'price' => $product->price,
'color' => (isset($_POST['color'])) ? $_POST['color'] : '',
'size' => (isset($_POST['size'])) ? $_POST['size'] : '',
]);
}
这是我到目前为止发布的代码,以便添加'没有刷新但不确定它是否正确。
$('.add-to-cartnon').on('click', function(){
var $btn = $(this);
var id = $btn.parent().parent().find('.product-id').val();
var color = $btn.parent().parent().find('.color').val() || '';
var qty = $btn.parent().parent().find('.quantity').val();
var size = $btn.parent().parent().find('.size').val() || '';
var add= $btn.parent().parent().find('.add').val() || '';
$.ajax ({
method: 'post',
url: 'example.php',
data: {
id: id,
color: color,
qty: qty,
size:size,
add:add
},
success: function(data) {
}
});
如果我想通过添加发布和刷新来完成这项工作:
var $form = $('<form action="?a=cart" method="post" />').html('<input type="hidden" name="add" value=""><input type="hidden" name="id" value="' + id + '"><input type="hidden" name="color" value="' + color + '"><input type="hidden" name="size" value="' + size + '"><input type="hidden" name="qty" value="' + qty + '">');
$('body').append($form);
$form.submit();
答案 0 :(得分:0)
$('.add-to-cartnon').on('click', function () {
var $btn = $(this);
var id = $btn.parent().parent().find('.product-id').val();
var color = $btn.parent().parent().find('.color').val() || '';
var qty = $btn.parent().parent().find('.quantity').val();
var size = $btn.parent().parent().find('.size').val() || '';
var dataArray = {id: id, color: color, qty: qty, size: size};
$.ajax({
method: 'post',
url: 'example.php',
data: {
add: dataArray
},
success: function (data) {
}
});
});