2015-05-06 102 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