Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 数量和删除购物车中的项目_Javascript_Php_Jquery_Yii2_Product Quantity - Fatal编程技术网

Javascript 数量和删除购物车中的项目

Javascript 数量和删除购物车中的项目,javascript,php,jquery,yii2,product-quantity,Javascript,Php,Jquery,Yii2,Product Quantity,我将来自商店的“项目”存储在会话中的唯一数组中,并将其传递到购物车上,我希望为每个项目输入数量,如果我希望从购物车中删除项目,则从数组中删除。我为“加号”和“减号”制作了html表单和数量按钮,但我不知道如何在输入值随按钮变化时捕捉该值,然后将其与每个项目的成本相乘,以便稍后我可以得到总成本(在代码中可以看到,我为每个项目设置了value=“1”,因此每个项目的基值为1)。我还使用JS从删除的项目中删除了我想要的行,但也不知道如何从数组中删除该项目 这是我的购物车: <div class=

我将来自商店的“项目”存储在会话中的唯一数组中,并将其传递到购物车上,我希望为每个项目输入数量,如果我希望从购物车中删除项目,则从数组中删除。我为“加号”和“减号”制作了html表单和数量按钮,但我不知道如何在输入值随按钮变化时捕捉该值,然后将其与每个项目的成本相乘,以便稍后我可以得到总成本(在代码中可以看到,我为每个项目设置了value=“1”,因此每个项目的基值为1)。我还使用JS从删除的项目中删除了我想要的行,但也不知道如何从数组中删除该项目

这是我的购物车:

<div class="container">  
<div class="jumbotron">
    <div class="container text-center">
        <h3>Shopping Cart</h3>     
        <table  class="table table-striped" >
            <tr>
                <th class="items" > Product </th> 
                <th class="items"> Quantity </th>
                <th class="items"> Unit Price </th>
                <th>Total Cost</th>
                <th></th>
            </tr>
            <?php foreach ($selectedItems as $item): ?>
                <tr>
                    <th class="items"><?= $item->name ?></th>
                    <th class="items ">
                        <div>
                            <button type="button" class="sub" title="If u want less quantity">-</button>
                            <input type="text" value="1" id="quantity" class="quantity" name="quantity[]" onchange="quantityChange(this)" > 
                            <button type="button" class="add" title="If u want more quantity" >+</button>
                        </div>
                    </th>
                    <th id="price" class="items"> <?= $item->price ?></th>
                    <th><span class="allcost"> </span></th>
                    <th class="items">
                        <button class="remove_field " title="Click to delete product" >
                            <div class="glyphicon glyphicon-trash" ></div>
                        </button>
                    </th>
                </tr>
            <?php endforeach; ?>
        </table>
        <?= Html::a('Return to Shop', ['/stock/shop'], ['class' => 'btn btn-default']) ?>
        <?= Html::a('Checkout', ['/stock/checkout'], ['class' => 'btn btn-default']) ?>
    </div>
</div>
   public function actionCart() {
    Yii::$app->session->open();

    Yii::$app->session->open();
    $selectedItems = Yii::$app->session['selectedItems'];

    $stockItems = Stock::find()->where(['id' => $selectedItems])->all();


    $data = [
        'selectedItems' => $stockItems,
    ];

    return $this->render('cart', $data);
}

数量和成本解决方案:

在HTML代码中:

<?php foreach ($selectedItems as $item): ?>
                <tr class="product" data-price="<?= $item->price ?>">
                    <th class="items"><?= $item->name ?></th>
                    <th id="price" class="items middle-th "> <!-- the 3rd column -->
                        <div >
                            <button type="button" class="sub" title="If u want less quantity">-</button>
                            <input class="quantityTxt quantity " id="quantity" name="quantity[]" type="text" value="1" onchange="quantityChange(this)" > 
                            <button type="button" class="add" title="If u want more quantity" >+</button>
                        </div>
                    </th>
                    <th>       
                        <span class="productTotal"></span>       
                    </th>
                    <th id="price" class="items  ">
                        <button class="remove_field " data-value="<?= $item->id ?>" title="Click to delete product" >
                            <div class="glyphicon glyphicon-trash" ></div>
                        </button>
                    </th>
                </tr>
            <?php endforeach; ?>
<?php foreach ($selectedItems as $item): ?>
                <tr class="product" data-price="<?= $item->price ?>">
                    <th class="items"><?= $item->name ?></th>
                    <th id="price" class="items middle-th "> <!-- the 3rd column -->
                        <div >
                            <button type="button" class="sub" title="If u want less quantity">-</button>
                            <input class="quantityTxt quantity " id="quantity" name="quantity[]" type="text" value="1" onchange="quantityChange(this)" > 
                            <button type="button" class="add" title="If u want more quantity" >+</button>
                        </div>
                    </th>
                    <th>       
                        <span class="productTotal"></span>       
                    </th>
                    <th id="price" class="items  ">
                        <button class="remove_field " data-value="<?= $item->id ?>" title="Click to delete product" >
                            <div class="glyphicon glyphicon-trash" ></div>
                        </button>
                    </th>
                </tr>
            <?php endforeach; ?>
 $('.add').click(function () {
    var target = $('.quantity', this.parentNode)[0];
    target.value = +target.value + 1;
    updateTotal();
});
$('.sub').click(function () {
    var target = $('.quantity', this.parentNode)[0];
    if (target.value > 1) {
        target.value = +target.value - 1;
    }
    updateTotal();
});

function quantityChange(sender) {
    var quantity = $(sender).val();

    console.log(quantity);
}
;

var updateTotal = function () {

    var sum = 0;
    //Add each product price to total
    $(".product").each(function () {
        var price = $(this).data('price');
        var quantity = $('.quantityTxt', this).val();
        //Total for one product
        var subtotal = price * quantity;
        //Round to 2 decimal places.
        subtotal = subtotal.toFixed(2);
        //Display subtotal in HTML element
        $('.productTotal', this).html(subtotal);
    });
    // total
    $('.productTotal').each(function () {
        sum += Number($(this).html());
    });
    $('#sum').html(sum.toFixed(2));
};

//Update total when quantity changes
$(".product").keyup(function () {
    updateTotal();
});

//Update totals when page first loads
updateTotal();

// set this from local
$('span.productTotal').each(function () {
    $(this).before("&euro;");
});

// unit price
$('.product').each(function () {
    var $price = $(this).parents("div").data('price');
    $(this).before($price);
});