简体   繁体   English

如何在Smarty部分循环中使用JavaScript变量

[英]How to use javascript variable in smarty section loop

I have a table with id = 2 and I want to loop over it 我有一个ID = 2的表,我想遍历它

I don't know the syntax , couldn't find anything similar on the net please help : 我不知道语法,在网上找不到类似的东西,请帮忙:

<script type="text/javascript">
var u=$('#2').find('tbody > tr').size();
<%section name='i' start=0 loop=u%>
  alert('in loop');
  <%/section%>
</script> 

You should look in your generated source to see that the browser (client) just gets HTML and no smarty (server). 您应该查看生成的源,以查看浏览器(客户端)仅获取HTML而没有智能(服务器)。 What you should do is use JavaScript to write your loop and then do everything client side. 您应该使用JavaScript编写循环,然后在客户端进行所有操作。

<script type="text/javascript">
var u = $('#2 tbody > tr').each(function() {
    alert('in loop');
});
</script> 

By the way, "2" is not a correct ID, it should always start with a letter. 顺便说一句,“ 2”不是正确的ID,它应始终以字母开头。

You can do this entirely on client side. 您可以完全在客户端执行此操作。 You need to differentiate on the code running your server, and running on your client, that is the browser. 您需要区分运行服务器和运行在客户端(即浏览器)上的代码。

When you want to manipulate a HTML table exists entirely inside browser, the client, you use client side code to work with it, that is Browser's language "JavaScript" which is fully capable of handling a loop by it's own. 当您要操纵一个完全存在于浏览器内部的HTML表时,客户端将使用客户端代码来使用它,即浏览器的语言“ JavaScript”,它完全能够自己处理循环。 No need to use Smarty there. 无需在那里使用Smarty。

You should invest sometime to learn the basics of JavaSciript. 您应该花一些时间来学习JavaSciript的基础知识。

I'll give you http://www.w3schools.com/js/ as a starting point, please do not consider this as an excellent place to learn everything about JavaScript but to get around basics it should be okay. 我将以http://www.w3schools.com/js/作为起点,请不要将其视为学习JavaScript的所有知识的绝佳去处,但是如果您了解基本知识,那应该没问题。

As per the looping example, I believe existing answer gives you a correct picture, except that it assigns the return value from each which I think you should remove, for easier understanding. 根据循环示例,我相信现有答案会为您提供正确的图片,只是它会为我认为应该删除的each值分配返回值,以便于理解。 However, having it will do no harm to you also. 但是,拥有它也不会对您造成伤害。

<script type="text/javascript">
   $('#2').find('tbody > tr').each(function() {
      alert('in loop');
      // You can access the current 'tr' element using $(this)

   });
</script> 

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

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