我有一个名为contact.html.twig的视图。它有一些带有文本框的表单。我想使用JavaScript来验证没有任何字段是空的,以及一些其他规则。但是我不知道把.js放在哪里。我不知道如何使用Twig表示法调用.js脚本。在Symfony2/Twig中使用javascript
11
A
回答
24
这是如何处理JavaScript的一个通用的答案......没有具体验证的一部分。我使用的方法是作为插件在束单独的JS文件存储各个功能Resources/public/js
目录,如下所示:
(function ($) {
$.fn.userAdmin = function (options) {
var $this = $(this);
$this.on('click', '.delete-item', function (event) {
event.preventDefault();
event.stopPropagation();
// handle deleting an item...
});
}
});
然后我用assetic包括在我的基本模板这些文件:
{% javascripts
'@SOTBCoreBundle/Resources/public/js/user.js'
%}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
在我的基本模板我在<body>
结束块为$(document).ready();
<script>
$(document).ready(function() {
{% block documentReady %}{% endblock documentReady %}
});
</script>
</body>
然后在我的网页具有“用户管理”功能,我可以调用userAdmin功能,像这样:
{% block documentReady %}
{{ parent() }}
$('#user-form').userAdmin();
{% endblock documentReady %}
1
相关问题
- 1. 在JavaScript中使用$
- 2. 在JavaScript中使用Javascript嵌入Javascript(Oi!)
- 3. 在JavaScript中使用javascript引用KnockoutJs值
- 4. 在html和javascript中使用jquery/javascript
- 5. 在javascript中使用javascript变量jsf
- 6. 在selenium中使用javascript javascript执行者
- 7. 以PHP格式在Javascript中使用Javascript
- 8. 在javascript中显示JSON使用javascript
- 9. 使用枚举在Javascript中
- 10. 在Javascript中使用函数
- 11. 在Perl中使用javascript CGI
- 12. 在Javascript中使用“this”
- 13. 在javascript中使用此类
- 14. 在django中使用javascript
- 15. 在javascript中使用原型
- 16. 在javascript中使用'=>'
- 17. 在javascript中使用数组
- 18. 在Javascript中使用.beget()
- 19. 在JavaScript中使用this.id?
- 20. 在javascript中使用servlet
- 21. Telnet在JavaScript中使用Applets
- 22. 在javascript中使用Html.DropDownList
- 23. 在UpdatePanel中使用ClientValidationFunction(JavaScript)
- 24. 在Jade中使用JavaScript
- 25. 在Javascript中使用PHP?
- 26. 在JavaScript中使用HTML
- 27. 在JavaScript中使用.JSON
- 28. 在Javascript中使用setimeout
- 29. 在Javascript中使用URL
- 30. 在javascript中使用map reduce
很不错的工作 - 谢谢 – someuser