javascript - 如何访问symfony2中另一个html.twig文件中的javascript函数?

标签 javascript html function symfony execution

我想知道如何访问 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/

相关文章:

javascript - 如何在 Shiny 中保存带有绘制形状/点的传单 map ?

javascript - React如何在ListItem之间每次切换时调用componentDidMount

Javascript 抽屉 slider

html - CSS全屏div,中间有文字

javascript - 如何将三个对象传递给只需要两个对象的函数?

javascript - 有人知道为什么这些元素会出现在移动网站上吗?

image - 使用 HTML5 拖放时,如何在拖动区域上方更改拖动图像?

java - 一个使用 Java 解决对数数学问题的程序

c++ - 我们可以有一个具有多种返回类型的函数吗? (C++11 及以上版本)

javascript - 随机 jQuery 幻灯片