简体   繁体   English

Ajax未加载内容

[英]ajax not loading content

I have a button for increasing the quantity 我有一个增加数量的按钮

<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.')" />

the function: 功能:

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

} }

and the product-change-quantity.php: 和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;

it's just not doing anything. 它只是什么也没做。 I would like to increase the quantity of a product with that button and later i would like to do more things in the background without refreshing the page so that's why i choosed ajax Thank you! 我想使用该按钮增加产品的数量,以后我想在后台执行更多操作而不刷新页面,所以这就是我选择Ajax的原因谢谢!

Based on the source codes you posted above, you're accessing an input element which is not in your html file. 根据您在上面发布的源代码,您正在访问不在HTML文件中的input元素。 If you can notice that in HTML, you have added space after the item id for the input element's ID. 如果可以注意到,在HTML中,您已在项目ID后面为输入元素的ID添加了空格。 While on your javascript, you didn't add space after item id when you are trying to access the same element although I think it is better if you don't put spaces in your ID. 在使用javascript时,尝试访问同一元素时,没有在项目ID后面添加空格,尽管我认为最好不要在ID中添加空格。

[HTML] [HTML]

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

[Javascript] [Javascript]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM