【发布时间】:2015-04-23 08:45:02
【问题描述】:
当我显示保存在会话中的选定产品时,我有一个按钮,如果我不需要它,我想使用它删除特定产品。我可以用javascript实现吗?如果不是这个问题的其他解决方案是什么?
我听说你不能用 javascript 设置会话变量,所以删除它们可能也是一样的,但我听说你可以用 ajax 做一些事情来删除它们?无论如何,我会像这样展示我的产品(现在我只动态显示产品的价格):
{% for item in items %}
<tr>
<td><img width="60" src="{{ asset('bundles/mpFrontend/assets/products/4.jpg') }}" alt=""/></td>
<td>{{ item.model }}</td>
<td>
<div class="input-append"><input class="span1" style="max-width:34px" placeholder="1" id="appendedInputButtons" size="16" type="text">
<button class="btn" type="button"><i class="icon-minus"></i></button>
<button class="btn" type="button"><i class="icon-plus"></i></button>
<button class="btn btn-danger" type="button" onclick="removeItem(item.id)"><i class="icon-remove icon-white"></i></button>
</div>
</td>
<td>$120.00</td>
<td>$25.00</td>
<td>$15.00</td>
<td>$110.00</td>
</tr>
{% endfor %}
更新这是我已经做的:
控制器中的removeAction:
public function removeAction($itemId)
{
$session = $this->getRequest()->getSession();
$session->remove();
return $this->render('MpShopBundle:Frontend:product_summary.html.twig');
}
控制器路由:
removeItem:
pattern: /remove
defaults: { _controller: MpShopBundle:Homepage:remove }
脚本:
<script>
$(".btn btn-danger").click(function(){
var itemId = $(this).val();
$.ajax({
type: "POST",
url: "{{ path('removeItem') }}",
data: { itemId: itemId }
});
</script>
按下按钮没有做任何事情,我并不感到惊讶,因为这是我第一次真正使用 javascript,我想我做错了什么?
【问题讨论】:
标签: javascript php symfony session