【问题标题】:Alternate approach to <span class="item-in-loop" data-index="{^{:#index}}"></span><span class="item-in-loop" data-index="{^{:#index}}"></span> 的替代方法
【发布时间】:2015-06-27 15:55:26
【问题描述】:

很抱歉提出了一个愚蠢的问题。我觉得必须有更好的方法,但我没有看到它。我不想在按钮单击中循环查找 ID。我只想传递要删除的索引。

这就是我认为我必须做的事情

html

<div id="list">
   {^{for items}}
      <div class="list-item">
         ...
         <a href="#" class="remove-list-item">Remove
            <span class="remove-list-item-index hide">{^{:#index}}</span>
         </a>
      </div>
   {{/for}}
</div>

js

$('a.remove-list-item').click(function () {  
   $.observable(listArray).remove(parseInt($(this).children('span.remove-list-item-index').text()));
});

这就是我想做的事情

html

<div id="listing">
   {^{for items}}
      <div class="listing-item"> 
         ...
         <a href="#" data-index="{^{:#index}}">Remove</a>
      </div>
   {{/for}}
</div>

js

$('a.remove-list-item').click(function () {  
   $.observable(listArray).remove(parseInt($(this).attr('data-index')));
});

【问题讨论】:

    标签: jsrender jsviews


    【解决方案1】:

    您可以使用var view = $.view(element) 获取任何元素的当前视图。

    对于 {^{for items}} 块对应的视图,每个视图都有一个索引属性。

    所以你可以这样做:

    $('a.remove-list-item').click(function () {  
       $.observable(listArray).remove($.view(this).index);
    });
    

    这比您建议的任何一种方法都简单。

    有许多样本使用这种方法,例如http://www.jsviews.com/#samples/editable/tags

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 2023-03-10
      • 2012-11-11
      • 1970-01-01
      • 2015-11-27
      • 1970-01-01
      • 2021-12-25
      相关资源
      最近更新 更多