简体   繁体   English

如何在smarty模板中使用js?

[英]how to use js in smarty template?

Using html_options how to add id or class attribute to it and how to use javascript in smarty specifically I need to perform some action on html_option change. 使用html_options如何向其中添加id或class属性以及如何在smarty中使用javascript,我需要对html_option更改执行一些操作。 in my .php file i have the following piece code: 在我的.php文件中,我具有以下片段代码:

<script ....>
function changeProvince(){
        var province = document.getElementById("addr_province");
        province.addEventListener("change", function(){ alert("Hello World!"); });
    }
changeProvince();
</script>

and in my .tpl file 并在我的.tpl文件中

{{html_options name=addr_province options=$addr_province id="addr_province"}}

With the above piece of code it is not working. 使用上面的代码,它不起作用。

There is no problem to use: 使用没有问题:

{html_options name=addr_province options=$addr_province id="addr_province"}

<script type="text/javascript">
function changeProvince(){
        var province = document.getElementById("addr_province");
        province.addEventListener("change", function(){ alert("Hello World!"); });
    }
changeProvince();
</script>

but as you see you need to create first options, and then run changeProvince(); 但是如您所见,您需要先创建选项,然后运行changeProvince(); JavaScript function. JavaScript函数。

Other way you get JavaScript error TypeError: province is null and simple it won't work. 您收到JavaScript错误TypeError: province is null其他方法TypeError: province is null ,简单的做法将不起作用。

EDIT 编辑

If you want, you can do it also this way: 如果需要,也可以通过以下方式进行操作:

<script type="text/javascript">
function changeProvince(){
        var province = document.getElementById("addr_province");
        province.addEventListener("change", function(){ alert("Hello World!"); });
    }
</script>

{html_options name=addr_province options=$addr_province id="addr_province"}

<script type="text/javascript">
changeProvince();
</script>

You can define your function earlier but run it after creating select and options 您可以更早地定义函数,但是在创建select和options后运行它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM