2012-12-04 151 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);