0

数量を増やすためのボタンがあります

<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 を選択しました ありがとう!

4

1 に答える 1

0

上記のソース コードに基づいて、html ファイルにない入力要素にアクセスしています。HTML で、項目 ID の後に入力要素の ID のスペースが追加されていることに気付いた場合。あなたのJavaScriptでは、同じ要素にアクセスしようとしているときにアイテムIDの後にスペースを追加しませんでしたが、IDにスペースを入れない方が良いと思います.

[HTML]

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

【ジャバスクリプト】

$('#quantity-' + item_id).val( new_quantity );
于 2012-12-05T05:39:44.393 に答える