2016-12-15 69 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

是的,我明白我的错误。谢谢。 –