簡體   English   中英

Ajax未加載內容

[英]ajax not loading content

我有一個增加數量的按鈕

<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.' );
        }
    }
});

}

和product-change-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;

它只是什么也沒做。 我想使用該按鈕增加產品的數量,以后我想在后台執行更多操作而不刷新頁面,所以這就是我選擇Ajax的原因謝謝!

根據您在上面發布的源代碼,您正在訪問不在HTML文件中的input元素。 如果可以注意到,在HTML中,您已在項目ID后面為輸入元素的ID添加了空格。 在使用javascript時,嘗試訪問同一元素時,沒有在項目ID后面添加空格,盡管我認為最好不要在ID中添加空格。

[HTML]

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

[Javascript]

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM