2016-12-15 65 views
-1

我無法將複選框選中爲取消選中複選框。將選中的複選框設置爲取消選中

$("#search_list").find('.mselect input:checked').each(function(index, ele) { 
      var prodId = $(this).val(); 
      console.log($(this)); 
      //if($(this).is(':checked')){ 
      //if($(this).length) { 
       $(productList).each(function(key, value) { 
        console.log("ohh"+value); 
        if(prodId == value){ 
         $(this).prop('checked', false); 
         console.log($(this)); 
         console.log("unchedk"+value); 
        } 
       }); 
      //} 
     }); 

我的錯誤是什麼?

+0

您應包括控制你想改變你可以在瀏覽器控制檯中看到的HTML,以及任何錯誤消息的變量。 – Tony

回答

0

上下文$(this)涉及productList的當前元素/屬性。指定複選框的$(this)像(去掉註釋代碼)

$("#search_list").find('.mselect input:checked').each(function(index, ele) { 

    var $checkbox = $(this), // now use $checkbox instead of $(this) in the next loop 
          // (the dollar sign is to indicate that the variable is alreay a jQuery object). 
     prodId = $(this).val(); 

    $(productList).each(function(key, value) { 

     if (prodId == value) { 
      $checkbox.prop('checked', false); 
     } 

    }); 

}); 
+0

@Andreas,你是對的。我想我學習的這個會議有一個缺陷。 –

+0

是'$ this'不是指複選框。它指的是productList的數組。 –

+0

是的,我明白我的錯誤。謝謝。 –