成功后,Symfony2 Ajax更改为完整的DIV



所以我有一个DIV显示来自数组的信息。使用我的Ajax脚本,我正在增加数组中的价值,我希望整个Div相应地更改...我已经进行了这些更改,但是我不知道如何更改整个DIV?我会使用.attr还是其他?

这是树枝:

{% for key, item in cart %}
                {% if key == info.id %}
                    <div class="input-append">
                    <input class="span1" style="max-width:34px" placeholder="{{ key }}" value="{{ item }}" id="appendedInputButtons" size="16" type="text" data-id="{{ key }}"/>
                    <a href="javascript:void(0)" class="remove btn"><i class="icon-minus"></i></a>
                    <a href="javascript:void(0)" class="add btn"><i class="icon-plus"></i></a>
                    <button class="btn btn-danger" type="button"><a href="{{ path('cart_remove', {'id': key}) }}"><i class="icon-remove icon-white"></i></button>
                    </div>
                  </td>
                  <td>{{ info.getQtyPrice(item)|number_format(2, '.', ',')}}</td>
                  <td>{{ info.getQtyDiscount(item)|number_format(2, '.', ',')}}</td>
                  <td>{{ info.getQtyTax(item)|number_format(2, '.', ',') }}</td>

                  <td>{{ info.getQtyFinal(item)|number_format(2, '.', ',') }}</td>

                </tr>
         {% endif %}
        {% endfor %}

使用Ajax我正在增加或减少数量(数量是可变项)。当我增加或减少数量时,我希望整个DIV都相应地更改。例如,如果产品数量为1,价格为50,当我增加数量时,我希望价格更改为100。这是我的脚本:

$(document).ready(function () {
    $(document).on('click', '.add', function (e) {
    $this = $(this);   
    $.ajax({
        type: 'POST',
        url: 'add/quantity',
        dataType: 'JSON',
        data: {product: $this.parent('.input-append').find('input').data('id'),quantity: $this.parent('.input-append').find('input').val()},
        success: function (data) {      
          if(data.success == false){
           alert('error')
          }else{
            $this.parent('.input-append').val(data.amount)  
           }
        }
    });
});

现在,线:$this.parent('.input-append').val(data.amount)仅更改输入字段中的数量。

我如何用价格更改整个DIV?

答案

感谢您的帮助。此代码可以解决问题: $('.table').load(" .table");

添加一个类似于这样更新的TD的类:

                  <td class="qtyprice">{{ info.getQtyPrice(item)|number_format(2, '.', ',')}}</td>
                  <td class="qtytax">{{ info.getQtyDiscount(item)|number_format(2, '.', ',')}}</td>
                  <td>{{ info.getQtyTax(item)|number_format(2, '.', ',') }}</td>
                  <td class="qtyfinal">{{ info.getQtyFinal(item)|number_format(2, '.', ',') }}</td>

和您的ajax,您可以通过类选择来相应地更新这些字段:

success: function (data) {      
          if(data.success == false){
           alert('error')
          }else{
            $this.closest('tr').find('.input-append').val(data.amount) ;
            $this.closest('tr').find('.qtyprice').val(data.qtyprice) ;
            $this.closest('tr').find('.qtytax').val(data.qtytax) ;
            $this.closest('tr').find('.qtyfinal').val(data.qtyfinal) ; 
           }
        }

您的AJAX响应数据需要获得成功的所有价格。

最新更新