【问题标题】:Symfony2 updating the values and displaying them without refresh with ajaxSymfony2 更新值并显示它们而不用 ajax 刷新
【发布时间】:2015-05-06 17:35:28
【问题描述】:

首先我想说我不是前端专家,这是我第一次深入研究 ajax。所以这就是我想做的:

我想在不刷新整个页面的情况下更改并显示更新的值。我唯一要更新的是输入字段及其值,仅此而已。如何只更新那部分代码?

这是我的树枝(当我按下 + 按钮时,我会增加产品的数量,并且我希望树枝更新它而不刷新整个页面。就像在任何标准购物车中一样):

{% 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>      
    {% endif %}         
{% endfor %}

这是脚本:

$(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{
            document.location.reload(true);
           }
        }
    });
});   
});

目前我只有 document.location.reload(true); 可以重新加载整个页面,我该如何更改该代码?

动作:

public function addQuantityAction( Request $request ) {
    $response = new JsonResponse();
    $requestData = $request->request->all();
    $productid     = $requestData['product'];
    $quantity = $requestData['quantity'];
    /** logic*/
    $em = $this->getDoctrine()->getManager();
    $product = $em->getRepository('MpShopBundle:Product')->find($productid);
    $qtyAvailable = $product->getStock();
    $session = $this->getRequest()->getSession();
    $cart = $session->get('cart', array());
    if ( $qtyAvailable > $cart[ $productid ] ) {
        $cart[ $productid ] = $cart[ $productid ] + 1;
        $qtyAvailable = $qtyAvailable - 1;
        $response->setData(array('success'=>true,'message'=>'Qunatity increased'));
         $session->set('cart', $cart);
    } else {
        $response->setData(array('success'=>false,'message'=>'Out of stock'));
    }
    return $response;
}

【问题讨论】:

    标签: javascript php jquery ajax symfony


    【解决方案1】:

    您可以在添加/数量操作的响应中返回新值,并在页面上放置您想要的位置:

    success: function (data) {      
          if(data.success == false){
           alert('error')
          }else{
            // Assuming the value on data.amount
            $('.input-data input[type=text]').val(data.amount);
           }
        }
    

    如果您愿意,请告诉我您的操作代码(添加/数量),并提供帮助。

    希望对你有帮助

    更新:

    在你的行动回应中:

    $response->setData(array(
        'success' => true,
        'message' => 'Qunatity increased',
        'amount'  => $cart[ $productid ]
    ));
    

    【讨论】:

    • 我也更新了我的答案。如果对你有用,请接受。谢谢!
    • 它不会在不刷新的情况下改变... :(
    • 可能这一行是错误的:$('.input-data input[type=text]').val(data.amount);它的输入附加,而不是输入数据。无论如何,您在模板中有新值,因此您应该可以在任何地方使用它。方式取决于您的 DOM。
    • 嘿,我有问题。您提供的代码有效,但它增加了所有产品的价值,而不仅仅是我按下的那个,刷新后一切都修复了..我怎样才能让它只改变我按下按钮的产品的输入开吗?
    • 将选择器改为$this.parent('.input-append').find('input').val(data.amount)
    猜你喜欢
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    相关资源
    最近更新 更多