2014-09-29 64 views
0

在我的应用程序中,我试图用if部分检查2个条件。但不工作。我没有得到的结果太..下划线模板 - 如何检查多个条件

这里是我的病情,我用什么:

<% _.each(recourseParameter, function (item, index) { %> 

      <% if (index % limit == 0) { %> 
       <%= _.template($('#header').html())({"parameterCode":parameterCode}) %> 
      <% } %> 

      <% if(item.description) { %> 
       <tr> 
        <td colspan=5><%= item.description %></td> 
       </tr> 
      <% } %> 

      <% if (((index+1) % limit == 0) && ((index +1) != limit)) { %> // this is not working! 
        <%= _.template($('#closer').html())() %> 
         <span>Continue ... </span> //this is not printing.. 
        </div> 
      <% } %> 

    <% }) %> 

Live Demo

+0

你介意检查什么是在控制台日志中的错误?我得到这个错误:无法加载资源:net :: ERR_TUNNEL_CONNECTION_FAILED https://dl.dropboxusercontent.com/u/6257603/recourseParameter.json 错误:错误,但它可能是网络在这里无法连接到保管箱。我会在家里再试一次。在你身边可以吗? – Hatjhie 2014-09-29 05:50:22

+0

这对我来说工作得很好。可能是这个URL被禁止给你。如果你希望让我给你基于静态数据的例子? – 3gwebtrain 2014-09-29 06:16:18

+0

@Hatjhie - 这里是放置在小提琴演示中的数据:http://jsfiddle.net/ffk5raqL/4/ – 3gwebtrain 2014-09-29 06:20:37

回答

1

的实际上是否工作正常。 if代码试图验证的逻辑不正确。

让我们来看看。

<% if (((index+1) % limit == 0) && ((index +1) != limit)) 

The possible value of index is 0 to 10 while the limit is always 9

现在满足该要求(index+1) % limit == 0 索引必须是8,其他比%将不为0,因为只有(8+1) % 9 == 0.

现在,索引为8。 第二条件if是(index+1) != limit其中(8 + 1)!= 9总是返回false。

因此,<% if (((index+1) % limit == 0) && ((index +1) != limit))永远不会返回true。

综上所述,如果功能正常使用,请修改相应的代码的逻辑:

+0

你有什么想法来修改这个逻辑。因为这个模板对我来说是非常新的..我尝试过一些后我得到了一个最好的想法.. – 3gwebtrain 2014-09-29 08:36:30

+0

这取决于你想要达到的目标。创建第三个的初衷是什么? – Hatjhie 2014-09-29 09:44:47

+0

我正在分组中的数据,我定义了“极限”。当“limit”是3时,我需要打印3组,并且需要说'继续'。当循环到达最后一组时,继续变成全部完成。 用户可以根据自己的要求定义“限制”。这就是我想用条件打印的东西。 – 3gwebtrain 2014-09-29 10:25:22