我想知道如何访问 symfony2 中另一个 html.twig 文件中的 javascript 函数。假设我们有两个 html.twig 文件: file1.html.twig 和 file2.html.twig 。我们还假设他们的代码如下:
file1.html.twig的代码:
<html>
<head>
<script>
function validate(){
//the code that I need to put here to run the javascript function executer() which exists in file2.html.twig
}
</script>
</head>
<body>
<form id="EventForm" action='{{path('ikproj_groupe_homepaeventsAdd',{id:idg})}}' method="POST" {{ form_enctype(form) }} onsubmit="validate();">
//here is the form content.
</form>
</body>
</html>
file2.html.twig的代码:
<html>
<head>
<script>
function executer(){
//the function content
}
</script>
</head>
<body>
</body>
</html>
实际上,我想做的是,提交 file1.html.twig 中的表单后,文件 file2.html.twig 中将执行函数executer()。在 Symfony2 中是否可以做到这一点?...如果可以,我应该在文件 file1.html.twig 的 validate() 函数中放入什么?
最佳答案
您可以将 javaScript 放入它自己的 twig 文件中,并根据需要将其包含在其他 twig 文件中。
示例(传递参数只是为了完整性);
executer.html.twig
<script>
function validate(){
// javascript function executer()
var foo = {{ foo }};
}
</script>
文件1.html.twig
<html>
<head>
{% include "executer.html.twig" with {'foo': 10} %}
</head>
<body>
<form id="EventForm" action='{{path('ikproj_groupe_homepaeventsAdd',{id:idg})}}' method="POST" {{ form_enctype(form) }} onsubmit="validate();">
//here is the form content.
</form>
</body>
</html>
文件2.html.twig
<html>
<head>
{% include "executer.html.twig" with {'foo': 20} %}
</head>
<body>
</body>
</html>
关于javascript - 如何访问symfony2中另一个html.twig文件中的javascript函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25683340/