2016-11-15 225 views
1

我有一個HTML結構,我想刪除一個額外的項目。刪除標記時刪除標記

<div class="tokenfield form-control"> 
    <input class="pro-credit-user" name="pro-credit-users[]" placeholder="Person or business name" style="position: absolute; left: -10000px;" tabindex="-1" type="text"> 
    <input style="position: absolute; left: -10000px;" tabindex="-1" type="text"> 
    <div class="token invalid"> 
     <span class="token-label" style="max-width: 152px;">dfgdfgdf</span> 
     <a href="#" class="close" tabindex="-1">×</a> 
    </div> 
    <input class="token-input ui-autocomplete-input" autocomplete="off" placeholder="Person or business name" id="1479230390171168-tokenfield" tabindex="0" style="min-width: 60px; width: 1237.4px;" type="text"> 
</div> 

<a class="delete-pro-credit" href="#">Delete</a> 
<span class="pro-credit-error" style="color:red;float:right">Sorry, this user cannot be found. We will not be able to link this persons name with an Enjoius account</span> 

我有jQuery的代碼也:

$("input.pro-credit-user").last().on('tokenfield:removetoken', function (e) { 
    $(this).closest('span').remove(); // not working 
}) 

當用戶刪除令牌我也想刪除它有一流的「親信貸錯誤」的跨度。我寫了jQuery,但它不工作。

如何刪除包含'pro-credit-error'類的範圍?

+0

在你的代碼中'.pro-credit-user'沒有父'span'元素? – adeneo

+0

什麼是''tokenfield:removetoken''? – j08691

+0

@ j08691它與http://sliptree.github.io/bootstrap-tokenfield/#examples –

回答

0

下面的代碼對我的作品:

$("input.pro-credit-user").last().on('tokenfield:removetoken', function (e) { 
      $(e.relatedTarget).parent().prev().parent().find('span.pro-credit-error:eq(0)').remove(); 
      }) 
0

closest通過遍歷其DOM樹中的祖先來工作。在這種情況下,你要刪除的span不是它的祖先

雖然沒有測試過,你可以試試這個

$("input.pro-credit-user").last().on('tokenfield:removetoken', function (e) { 
       $(this).parent().sibling('span.pro-credit-error').remove(); 
      }) 
+0

不適合我! –

0

試試這個

$(this).next('span').remove(); 
+2

輸入沒有子女 – adeneo

+0

無法正常工作! –

+0

你想刪除這個div內的span嗎?

dfgdfgdf ×