Symfony2 Jquery 没有做任何事
Symfony2 Jquery not doing anything
所以我用 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) 我在 twig 中添加了代码以使脚本工作:
<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 库添加到我的资产中:
{% 阻止 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:dump :
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 ]
这就是我所做的,代码仍然不起作用。转储命令没有给我任何错误,这意味着路由是正确的。但是脚本不起作用...
您需要将 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())
});
});
所以我用 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) 我在 twig 中添加了代码以使脚本工作:
<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 库添加到我的资产中:
{% 阻止 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:dump :
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 ]
这就是我所做的,代码仍然不起作用。转储命令没有给我任何错误,这意味着路由是正确的。但是脚本不起作用...
您需要将 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())
});
});