2016-11-11 54 views
1

如果你能幫我解決我的問題,請諒解。我有20-30元件這樣的一個:jQuery /標籤輸入 - 按內容選擇元素

<div class="file file-unchosen"> 
    <div class="file-name-title"> 
    "File-title" 
    </div> 
</div> 

其中.file用於塊的CSS的造型

,我已經bootstrap tags input list哪裏可以存儲選擇了與功能文件:

$('.file').click(function() { 
    $(this).toggleClass('file-unchosen file-chosen'); 
    function addWork(elem) { 
    var workname = $(elem).find('.file-name-title').text(); 
    $('#chosen-works-list').tagsinput('add', workname); 
    }; 
    addWork(this); 
}); 

問題是,當我從標籤輸入列表中刪除文件時,我無法理解如何使.file選擇againg。現在,我在這樣的事情中(不反正工作):

$('#chosen-works-list').on('itemRemoved', function(event) { //when tag is removed from tags list 
    var chosenWorks = []; 
    $('.file-chosen').each(function(i,elem) { //we take all of the chosen files 
    console.log(elem); //log them 
    chosenWorks.push($(elem).find('.file-name-title').text().toString()); 
       }); //push them to array 
    var delWork = event.item; //define deleting tag 
    ...and I don't know what to do next.. 
}); 

從理論上講,下一步就要檢查一下是否$的.text()等於$(文件選擇.file名。) (delWork)並切換其類,但無法弄清楚如何做到這一點。

回答

1

試試這個:

<div class="file file-unchosen" data-content="deleted-tag-content"> 

然後就可以像這樣:

$('.file[data-content="'+ delWork +'"]').toggleClass('file-unchosen file-chosen'); 

不要毫不猶豫地使用數據屬性,並通過他們的查詢。

相關問題