2014-11-06 101 views
0

當我點擊複選框時,它會起作用,但是當我點擊它時,它不起作用,checkboxSelected類不會刪除。jQuery for checkbox在第一次點擊後不起作用

HTML標記:

點擊後
<div class="galleryMenu-center"> 
       <div class="galleryMenu-row" onclick="caruselFilter('.filter1984');"> 
        <div class="checkbox"></div> 
        <div class="thisText">1984</div> 
       </div> 
       <div style="clear:both;"></div> 
    </div> 

HTML標記:

<div class="checkbox checkboxSelected" style="background-position: 0px -19px;"></div> 

這是我的jQuery代碼:

$('.galleryMenu-row').click(function(){ 
     if($(this).find(".checkbox").hasClass('checkboxSelected')){ 
      $(this).find(".checkbox").removeClass('checkboxSelected'); 
      $(this).find(".checkbox").css('background-position', '0px -19px'); 
      var temcurrentShow = str_replace("," + currentClasa, "", currentShow); 
      $(".imgSlideshow").css('display', 'block'); 
      //$(temcurrentShow).css('display', 'block'); 
     } 
     $(this).find(".checkbox").addClass('checkboxSelected'); 
    }); 

問候

+2

'$(這).find( 「複選框。 」)removeClass( 'checkboxSelected');'移除類和'$(本).find(「。複選框」)addClass ('checkboxSelected');'再次添加。你能指望什麼?看來你需要一個'else'塊! – undefined 2014-11-06 13:22:45

+0

爲什麼你不使用複選框?簡單的CSS將允許您擁有您正在使用的圖像。 – epascarello 2014-11-06 13:30:44

+0

str_replace不是JavaScript函數。使用'var temcurrentShow = temcurrentShow.replace(「,」+ currentClasa,「」,currentShow);' – Mephiztopheles 2014-11-06 15:34:07

回答

0

是的,因爲你是一直添加s點擊:$(this).find(".checkbox").addClass('checkboxSelected');

也許你想這?

if ($(this).find(".checkbox").hasClass('checkboxSelected')) { 
    //... 
} else { 
    $(this).find(".checkbox").addClass('checkboxSelected'); 
} 
0
$(this).toggleClass("checkboxSelected"); 
+0

'(「'checkboxSelected'」);'是錯誤的...刪除一組引號。 – epascarello 2014-11-06 13:33:19

+0

謝謝epascarello ...斑點 – Billy 2014-11-06 13:36:45

相關問題