dqf42223 2013-06-08 17:38
浏览 207

无法编译.tpl文件中的JS

I have the following code inside my .tpl file (I am using the Smarty template engine for my project).

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
   <script>
          $(document).ready(function(){
    $('.check:button').toggle(function(){
        $('input:checkbox').attr('checked','checked');
        $(this).val('uncheck all');
    },function(){
        $('input:checkbox').removeAttr('checked');
        $(this).val('check all');        
    })
})
</script>

<input type="button" class="check" value="check all" />

   <input type="checkbox" class="cb-element" /> Checkbox  1
   <input type="checkbox" class="cb-element" /> Checkbox  2
   <input type="checkbox" class="cb-element" /> Checkbox  3

However, in the final compiled php code, the JS function doesn't appear, as a result of which the toggle ("check all") button I want to create doesn't work. The final compiled php code, as seen in the browser looks like this :

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
   <script>
          $(document).ready(function(),function())
})
</script>

<input type="button" class="check" value="check all" />

   <input type="checkbox" class="cb-element" /> Checkbox  1
   <input type="checkbox" class="cb-element" /> Checkbox  2
   <input type="checkbox" class="cb-element" /> Checkbox  3

I am new to using both jQuery and Smarty. Any help would be greatly appreciated. The jQuery function has been discussed in this SO post.

EDIT : I have put the JS inside {literal} and {/literal} and I can now see the JS function in the finally compiled php code. However, the JS function still does not respond! Why might that be?

  • 写回答

2条回答 默认 最新

  • dpi9530 2013-06-08 18:15
    关注

    Consider putting your JS in separate file and linking to it. Smarty uses { and } as special characters.

    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容