替换“;输入“;用一个简单的“;下拉框”;无法在php代码中工作

Replacement of "input" field by a simple "drop down box" not working in php code?

本文关键字:代码 工作 php 输入 替换 简单 一个      更新时间:2023-09-26

我遇到了一个无法解决的难题,这让我已经忙了一天。在我的电子商务Opencart2程序的结账页面中,人们在转移到支付页面之前,会在购物车中了解他们的产品概况。在结账页面上,他们仍然可以更改所需的产品数量。当他们更改数量时,他们必须单击刷新按钮来重新计算购物车总数。

在最初的opencart脚本中,可以通过一个简单的输入字段来更改数量。但我想用一个基于库存产品数量的下拉列表来代替这个数量输入字段(这样它们就永远不会超过库存数量)。我已经成功地将此应用于我的所有网站(中间购物车展示、产品页面等)。但只有在最后的结账页面上,它才不起作用。下拉列表很好地显示了1、2、3。。。最大数量)。

下拉示例

但是,当他们选择一个数量并单击刷新按钮时,他们会返回到购物车概述页面,告知购物车中不再有产品。

我认为数量没有(空)或没有正确(值为零)过账。请有人看看下面的脚本,看看是否有可能的代码错误?它把我逼疯了,它会让我开心的。此外,是否有一种简单的方法(mozilla插件)来检查在不同页面之间传递的数量值。

谢谢,SabKo

  1. 带有输入字段的cart.tpl页面中的原始代码有以下代码(并且正在工作):

                    <td class="text-left quantity">
                    <div class="input-group btn-block" style="max-width: 200px;">
                        <input type="text" name="quantity[<?php echo $product[version_compare(VERSION, '2.1', '<') ? 'key' : 'cart_id']; ?>]" value="<?php echo $product['quantity']; ?>" size="1" class="form-control" />
                        <span class="input-group-btn">
                            <button type="submit" data-toggle="tooltip" title="<?php echo $button_update; ?>" data-product-key="<?php echo $product[version_compare(VERSION, '2.1', '<') ? 'key' : 'cart_id']; ?>,<?php echo $product['quantity']; ?>" class="btn btn-primary btn-update" ><i class="fa fa-refresh"></i></button>
                            <button type="button" data-toggle="tooltip" title="<?php echo $button_remove; ?>" data-product-key="<?php echo $product[version_compare(VERSION, '2.1', '<') ? 'key' : 'cart_id']; ?>" class="btn btn-danger  btn-delete"><i class="fa fa-times-circle"></i></button>
                        </span>
                    </div>
                </td>
    
  2. 我已经更改了cart.tpl页面中的脚本如下:

                    <td class="text-left quantity">
                     <div class="input-group btn-block" style="max-width: 200px;">
                         <select name="quantity[<?php echo $product[version_compare(VERSION, '2.1', '<') ? 'key' : 'cart_id']; ?>]" class="form-control_cart" id="input-quantity" value="<?php echo $product['quantity']; ?>" >
                         <?php foreach (range($product['minimum'], $product['stockhoeveelheid'], 1) as $stap) {
                              if ($stap == $product['quantity']) {
                                    echo "<option value='$stap' selected>$stap</option>";
                              } else {
                                    echo "<option value='$stap'>$stap</option>";
                              }
                            }  ?>
                         </select>
                        <span class="input-group-btn">
                            <button type="submit" data-toggle="tooltip" title="<?php echo $button_update; ?>" data-product-key="<?php echo $product[version_compare(VERSION, '2.1', '<') ? 'key' : 'cart_id']; ?>,<?php echo $product['quantity']; ?>" class="btn btn-primary btn-update" ><i class="fa fa-refresh"></i></button>
                            <button type="button" data-toggle="tooltip" title="<?php echo $button_remove; ?>" data-product-key="<?php echo $product[version_compare(VERSION, '2.1', '<') ? 'key' : 'cart_id']; ?>" class="btn btn-danger  btn-delete"><i class="fa fa-times-circle"></i></button>
                        </span>
                    </div>
                </td>
    
  3. 类"btn-update"在checkout.tpl:中启动以下脚本

    $(document).delegate('.checkout-product .input-group .btn-update', 'click', function () {
    var key = $(this).attr('data-product-key');
    var qty  = $('input[name="quantity[' + key + ']"]').val();
    $.ajax({
        url: 'index.php?route=journal2/checkout/cart_update',
        type: 'post',
        data: {
            key: key,
            quantity: qty
        },
        dataType: 'json',
        beforeSend: function() {
            triggerLoadingOn();
            $('#cart > button > a > span').button('loading');
            $('.checkout-cart').addClass('checkout-loading');
        },
        complete: function() {
            triggerLoadingOff();
            $('#cart > button > a > span').button('reset');
        },
        success: function(json) {
            setTimeout(function () {
                $('#cart-total').html(json['total']);
            }, 100);
            if (json['redirect']) {
                location = json['redirect'];
            } else {
                $('#cart ul').load('index.php?route=common/cart/info ul li');
                $(document).trigger('journal_checkout_reload_payment');
                $(document).trigger('journal_checkout_reload_shipping');
            }
        }
    });
    

    });

    $(document).delegate('.checkout-product .input-group .btn-delete', 'click', function () {
    var key = $(this).attr('data-product-key');
    $.ajax({
        url: 'index.php?route=journal2/checkout/cart_delete',
        type: 'post',
        data: {
            key: key
        },
        dataType: 'json',
        beforeSend: function() {
            triggerLoadingOn();
            $('#cart > button > a > span').button('loading');
            $('.checkout-cart').addClass('checkout-loading');
        },
        complete: function() {
            triggerLoadingOff();
            $('#cart > button > a > span').button('reset');
        },
        success: function(json) {
            setTimeout(function () {
                $('#cart-total').html(json['total']);
            }, 100);
            if (json['redirect']) {
                location = json['redirect'];
            } else {
                $('#cart ul').load('index.php?route=common/cart/info ul li');
                $(document).trigger('journal_checkout_reload_payment');
                $(document).trigger('journal_checkout_reload_shipping');
            }
        }
    });
    

    });

  4. 我的checkout.php页面上的cart_update代码如下:

    public function cart_update() {
    $key = Journal2Utils::getProperty($this->request->post, 'key');
    $qty = Journal2Utils::getProperty($this->request->post, 'quantity');
    $this->cart->update($key, $qty);
    $json = array();
    if (!$this->checkCart()) {
        $json['redirect'] = Journal2Utils::link('checkout/cart');
    } else {
        $json['total'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0), Journal2Utils::currencyFormat($this->model_journal2_checkout->getTotal()));
    }
    echo json_encode($json);
    exit;
    

    }

以下是您的问题:

// As you can see, you're still trying to fetch the value from the input/textbox when you've changed it to a select.
var qty  = $('input[name="quantity[' + key + ']"]').val();
// So change the above like so:
var qty  = $('select[name="quantity[' + key + ']"]').val();

希望这能有所帮助,你可以继续你的工作:)