2012-12-04 149 views
0

我有一个按钮用于增加数量AJAX不加载内容

<input name="quantity-"'.$item_id.'" " id="quantity-'.$item_id.' " type="text" value="' . $quantity . '" /> 

$increase = '1'; 

<input name="increase-' . $item_id . '" id="increase-' . $item_id . '" type="button" value="+" onClick="cart_change_quantity('.$item_id .','.$quantity.','.$increase.')" /> 

功能:

function cart_change_quantity(item_id, quantity, mod) { 
$.ajax({ 
    url: 'functions/product-change-quantity.php', 
    type: 'GET', 
    data: { 
     item_id:   item_id, 
     quantity:   quantity, 
     mod:    mod 
    }, 
    success: function(response) { 
     if (response.search('success') != -1) { 
      var felbontva = response.split('__'), 
       new_quantity = felbontva[1]; 

      $('#quantity-' + item_id).val(new_quantity); 
     } 

     else { 
      alert('An error occured while trying to update your order.'); 
     } 
    } 
}); 

}

和产品变化quantity.php:

$item_id = $_GET["item_id"]; 
$quantity = $_GET["quantity"]; 
$mod= $_GET["mod"]; 

    if ($mod == '1') { $new_quantity = (int)$quantity + 1; } 


    echo 'success__' . $new_quantity; 

它只是没有做任何物品ING。我想,以增加产品的数量与该按钮,后来我想这样做的背景更多的事情而无需刷新页面所以这就是为什么我的ajax选用 谢谢!

+2

你应该张贴更多的HTML/PHP的组合,因为它是相当不可读的。 – Ulflander

+0

你最喜欢的开发者控制台说什么?请求是否经过?什么是变数? – soulseekah

+0

您能告诉我们脚本功能/ product-change-quantity.php的内容吗? – Stegrex

回答

0

根据您在上面贴了,你所访问的输入元素这是不是在你的HTML文件的源代码。如果您注意到在HTML中,您已在输入元素的ID的项目标识之后添加了空格。尽管在你的javascript中,当你尝试访问相同的元素时,你并没有在项目ID之后添加空格,但我认为如果不在ID中放置空格会更好。

[HTML]

<input name="quantity-"'.$item_id.'" " id="quantity-'.$item_id.' " type="text" value="' . $quantity . '" /> 

[Java脚本]

$('#quantity-' + item_id).val(new_quantity);