我正在开发一个购物网站应用程序。在我的应用程序中,客户可以观看每日优惠。我在数据库中有一个名为offer的表。我正在使用Struts2框架。我有一个名为OfferView.java
的动作类。在这个课程中,我将所有优惠的结束日期添加到列表中。(List<String[]>
)。我的目的是,我需要根据每个优惠的结束日期运行倒数计时器。在我看来,我试图在Struts2迭代器中运行一个javascript。但我失败了。对于倒计时器,我正在使用jquery.countdown.js
。我试过这个 -
<s:iterator value="days">
<script type="text/javascript">
$(function () {
var day ='<s:property value="days[0]"/>';
var austDay = new Date();
austDay = new Date(day+1, - 1, 25);
$('#defaultCountdown').countdown({until: austDay});
$('#year').text(austDay.getFullYear());
});
</script>
</s:iterator>
这个迭代器正在工作。当我在脚本之外添加<s:property value="days[0]"/>
时,它运行良好。我怎么能在迭代器中运行一个javascript?是否可能,如果它是如何做到的?
答案 0 :(得分:0)
Strtus2正在服务器端运行,而javascript是基于客户端的东西所以你不能。在外面使用单独的Javascript块并根据id或名称soemthing访问这些东西。
如果你有一千个项目,那么Iterator将复制Javascript块一千次。
1, User Struts2 Tags to generate images(for example) like this,
<div id="item_here name or id based on the time"><img ></img></div>
2, In Javascript or Jquery use counter every time interval to find valid Items.
There get all items starting with the id "item_".
3,Now check its time with current time and if it is expired simply hide or
whatever you wish.
答案 1 :(得分:0)
尝试将函数放在迭代器之外,如下所示:
function countdown(day) {
var austDay = new Date();
austDay = new Date(day + 1, -1, 25);
$('#defaultCountdown').countdown({
until: austDay
});
$('#year').text(austDay.getFullYear());
}
然后在你的迭代器中尝试调用它:countdown('<s:property value="days[0]"/>');