2015-05-06 105 views
-1

所以我用jQuery做了一个简单的函数来检查它是否工作,它不是。我不知道为什么会发生这种情况,因为我在其他堆栈问题上找到了所有的东西。它只是没有做任何事情......Symfony2 jquery没有做任何事情

我的一切都已经完事了:

1)我创建的脚本,并在文件中保存它:

$('.plus').on('click', function (e) { 
    $this = $(this); 
    alert("product id "+$this.parent('.item').find('input').data('id') + " Quantity "+$this.parent('.item').find('input').val()) 
}); 

2)我添加的代码在树枝使脚本工作:

<div class="item"> 
    <input type="text" value="2" data-id="1" /> 
    <a href="javascript:void(0)" class="plus">+</a> 
    <a href="javascript:void(0)">-</a> 
</div> 
<div class="item"> 
    <input type="text" value="2" data-id="2" /> 
    <a href="javascript:void(0)" class="plus">+</a> 
    <a href="javascript:void(0)">-</a> 
</div> 
<div class="item"> 
    <input type="text" value="2" data-id="3" /> 
    <a href="javascript:void(0)" class="plus">+</a> 
    <a href="javascript:void(0)">-</a> 
</div> 

现在在我的base.html.twig我加入了jQuery库我assetics:

{%块的JavaScript%}

 {# Le Javascript #}   
      {% javascripts 

       'bundles/mpFrontend/assets/js/jquery-1.11.2.min.js' // the jquery library  

       %}  

       <script src="{{ asset_url }}"></script>  
      {% endjavascripts %} 

    {% endblock %} 

然后在我的索引HTML,在这里我想使脚本工作,我做的:

1)扩展基树枝:{% extends "::base.html.twig" %}

2)我访问的脚本:

{% block javascripts %} 
    {{ parent() }} 
    {% javascripts 'js/scripts.js'%} 
     <script type="text/javascript" src="{{ asset_url }}"></script> 
    {% endjavascripts %} 
{% endblock %} 

4)我做assetic:转储:

php app/console assetic:dump 

5)我加了捆绑在我config.yml,因为我得到了错误:You must add "" to the assetic.bundle config

assetic: 
    debug:   "%kernel.debug%" 
    use_controller: false 
    bundles:  [ MpShopBundle ] 

这是香港专业教育学院做了什么,而代码仍然不起作用。转储命令没有给我任何错误,这意味着路线是正确的。然而,脚本不工作...

回答

2

你需要包装你的JavaScript代码在jQuery的ready事件。

$(document).ready(function() { 
    $('.plus').on('click', function (e) { 
     $this = $(this); 
     alert("product id " + $this.parent('.item').find('input').data('id') + " Quantity " + $this.parent('.item').find('input').val()) 
    }); 
}); 
+0

好吧......到目前为止2015年最愚蠢的错误...谢谢! – Dominykas55