2012-11-27 55 views
0

這已修復。這是糟糕的編程。我正在調用這個函數兩次。所以它正在刪除類,但是當函數第二次運行時它正在讀取類。對不起,我的編程不好,謝謝大家的幫助。我愛所有有幫助的人都喜歡stackoverflow!jQuery hasClass返回true,但removeClass不起作用?

好吧,我一直在試圖找出一些東西,但它可能是我的冷,阻止我解決它。我正在做的是當用戶選擇一些它增加了一個類。但如果他們再次選擇它,我想刪除這個類。所以我查看了元素hasClass是否返回true,但是當我刪除類時,它什麼都沒做......

修改JS看起來像這樣。爲了表明我可以看到Selectedindex和index匹配,並且出於某種原因,正在跳過/忽略removeClass for「option-select」。我可以改變它說removeClass(「選項」),它工作得很好,但我不能removeClass(「選項選擇」)選擇的元素時

$(obj.find('.option')).each(function(Selectedindex) { 
    if ($(this).hasClass('option-selected') && Selectedindex == index) { 
     $(this).removeClass('option-selected'); 
     console.log(Selectedindex+" == "+index); 
    } 
}); 

指數傳遞給函數。

這裏是一些HTML

<div id="MultipleSelect-HTML" class="dropdown container" multiple="multiple" style="width: 100%;"> 
    <ul class="options" style="width: 100%; display: block; position: relative;"> 
     <li> 
      <a class="option option-selected"> 
       <input class="option-value" type="hidden" value="0"> 
       <img class="option-image" src="http://cdn1.iconfinder.com/data/icons/inside/PNG/032x032/icontexto-inside-facebook.png"> 
       <label class="option-text" style="cursor:pointer;">Facebook</label> 
       <small class="option-description desc">Check out my Facebook page!</small> 
      </a> 
     </li> 
     <li> 
      <a class="option option-selected"> 
       <input class="option-value" type="hidden" value="1"> 
       <img class="option-image" src="http://cdn1.iconfinder.com/data/icons/inside/PNG/032x032/icontexto-inside-twitter.png"> 
       <label class="option-text" style="cursor:pointer;">Twitter</label> 
       <small class="option-description desc">Check out my Twitter page!</small> 
      </a> 
     </li> 
     <li> 
      <a class="option"> 
       <input class="option-value" type="hidden" value="2"> 
       <img class="option-image" src="http://cdn1.iconfinder.com/data/icons/inside/PNG/032x032/icontexto-inside-linkedin.png"> 
       <label class="option-text" style="cursor:pointer;">LinkedIn</label> 
       <small class="option-description desc">Check out my LinkedIn page!</small> 
      </a> 
     </li> 
     <li> 
      <a class="option"> <input class="option-value" type="hidden" value="3"> 
       <img class="option-image" src="http://cdn1.iconfinder.com/data/icons/inside/PNG/032x032/icontexto-inside-flickr.png"> 
       <label class="option-text" style="cursor:pointer;">Flickr</label> 
       <small class="option-description desc">I don't have a flicker Page :(</small> 
      </a> 
     </li> 
    </ul> 
</div> 

的請保持所有這些信息都使用JavaScript動態

感謝您的幫助,您可以提供生成的頭腦。

+0

你可以發佈你的HTML。此外,您正在檢查'選擇選項'並刪除'dd-option-selected' - 是否正確? –

+0

你正在檢查的課程和你正在移除的課程是不同的。 – Bruno

+0

是的,修好了,謝謝...但它仍然無法工作。任何其他想法? –

回答

2

你正在做hasClass爲option-selected而是試圖removeClass dd-option-selected

假設它是dd-option-selected,改變像下面,

$(obj.find('.option')).each(function(Selectedindex) { 
    if ($(this).hasClass('dd-option-selected') && Selectedindex == index) 
     $(this).removeClass('dd-option-selected'); 
}); 

呀,固定的感謝...但它仍然沒有按」工作。任何其他想法? - 羅伯特·麥金託什

如果這DD-選項選擇的,它是不刪除類,然後將selectedIndex不等於指數 - 維加

+0

是的,修好了,謝謝...但它仍然無法正常工作。任何其他想法? –

+1

如果'this'有'dd-option-selected'並且它沒有刪除類,那麼'Selectedindex'不等於'index' –

+0

這就是我的想法,所以我添加了「console.log(Selectedindex +」 ==「+ index),我得到了像」1 == 1「這樣的東西,所以它找到了元素,我甚至可以執行」$(this).parent()。html()「並查看類本身。 –

1

你被hasClass option-selectedremoving dd-option-selectedyou should check dd-option-selected檢查。

你的條件是。

if ($(this).hasClass('dd-option-selected') && Selectedindex == index) 
     $(this).removeClass('dd-option-selected'); 

如果您不能確定類名和類名並不重要,那麼你可以使用removeAttr清除類

$(this).removeAttr('class'); 
+0

是的,修正了那個謝謝...但它仍然不起作用任何其他想法? –

+0

如果你想刪除類,不管名字,然後你可以使用$(this).removeAttr('class'); – Adil

+0

我不能刪除整個類只有一個類可以有幾個 –

相關問題