0

初心者の質問で申し訳ありません。もっと良い方法があるはずだと感じていますが、私はそれを見ていません。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')));
});
4

1 に答える 1

1

を使用して、任意の要素の現在のビューを取得できますvar view = $.view(element)

また、{^{for items}} ブロックに対応するビューの場合、各ビューには index プロパティがあります。

したがって、次のことができます。

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

これは、提案されたアプローチのいずれよりも簡単です。

http://www.jsviews.com/#samples/editable/tagsなど、そのアプローチを使用したサンプルが多数あります。

于 2015-07-09T21:24:36.507 に答える