重力在每个产品数量 jQuery 旁边形成小计



我正在使用重力表单产品字段来创建订单表单类型系统。

我正在尝试计算每个产品的小计(成本*数量),并通过数量旁边的jQuery显示它。

这是我的jQuery:

<script type="text/javascript">
    $(document).ready(function() {
        $('.gfield_price .ginput_container').append('<div class="product_subtotal">SUBTOTAL</div>');
        $('.ginput_quantity').keypress( function( ) {
            $x = $('.ginput_quantity');
            $x.next('div.product_subtotal').html( this.value );
        });
    });
</script>

这是我的 html 的一个片段:

<li id='field_5_12' class='gfield gfield_price gfield_price_5_12 gfield_product_5_12' >
    <label class='gfield_label' for='input_5_12'>500 GPH Bilge Pump 24V</label>
    <div class='ginput_container'>
        <input type='hidden' name='input_12.1' value='500 GPH Bilge Pump 24V' class='gform_hidden' />
        <span class='ginput_product_price_label'>Price:</span>
        <span class='ginput_product_price' id='input_5_12'>$2.99</span>
        <input type='hidden' name='input_12.2' id='ginput_base_price_5_12' class='gform_hidden' value='$2.99'/>
        <span class='ginput_quantity_label'>Quantity:</span>
        <input type='text' name='input_12.3' value='' id='ginput_quantity_5_12' class='ginput_quantity' size='10' tabindex='6'/>
    </div>
</li>

如您所见,我只是尝试使用输入到数量字段中的值更新product_subtotaldiv。我稍后会担心实际的计算。我的问题是我不确定如何只选择下一个div.product_subtotal因为 $x.next() 选择每个product_subtotal。

您的 next() 方法会选择所有具有 .product_subtotal 类的div 标签,因为您在调用 next() 方法之前选择页面上的所有.ginput_quantit输入,该方法选择所有具有 .product_subtotal 类的div 标签,请尝试以下操作:

$('.ginput_quantity').keypress( function( ) {
    $x = $(this); // just this input element
    $x.next('div.product_subtotal').html( this.value ); 
});

最新更新