2016-05-13 49 views
1

我正在嘗試爲網站製作一個濾鏡。在哪裏我想刪除可見的複選框(我得到那個工作),刪除標籤等,只留下一個背景顏色< - 這一切工作。但現在我不知道如何使這些div的點擊能夠輸入字段隱藏。有沒有辦法解決這個問題在jQuery中?jquery點擊一個隱藏的複選框

這裏是我的代碼:

function onDataReceived(data) { 
     $.each(data.colorInfo, function(key, colorInfo) { 
      var inputElement = $("input[name^=filter][value="+colorInfo.id+"]").css("display", "none"); 
      var labelElement = $('label[for^=filter]').remove(); 
      var div = inputElement.parent(); 

      div.attr('class', 'myclass_'+colorInfo.id); 
      div.css("background-color", colorInfo.colorCode); 
      div.css("width", "15px"); 
      div.css("height", "15px"); 
      div.css("display", "inline-block"); 
      div.css("marginLeft", "5px"); 
      div.css("marginBottom", "5px"); 
      //console.log(colorInfo); 
     }); 
    } 

    $(document).ready(function() { 
     var url = 'myapi.json'; 
     $.get(url, onDataReceived); 
    }); 

和HTML:

<div class="filter_3"> 
    <input name="filter[]" value="152194" type="checkbox"/> 
    <label for="filter_152194">Rood</label> 
</div> 
<div class="filter_4"> 
    <input name="filter[]" value="152196" type="checkbox"/> 
    <label for="filter_152196">Blauw</label> 
</div> 
<div class="filter_5"> 
    <input name="filter[]" value="152198" type="checkbox"/> 
    <label for="filter_152198">Oranje</label> 
</div> 
+0

試試這個答案的http:/ /stackoverflow.com/a/4177175/6127393輸入是隱藏的應該沒關係。 –

回答

2

假設有一次在div點擊調用的函數:

function onDataReceived(data) { 
    $.each(data.colorInfo, function(key, colorInfo) { 
     var inputElement = $("input[name^=filter] [value="+colorInfo.id+"]").css("display", "none"); 
     var labelElement = $('label[for^=filter]').remove(); 
     var div = inputElement.parent(); 

     div.attr('class', 'myclass_'+colorInfo.id); 
     div.css("background-color", colorInfo.colorCode); 
     div.css("width", "15px"); 
     div.css("height", "15px"); 
     div.css("display", "inline-block"); 
     div.css("marginLeft", "5px"); 
     div.css("marginBottom", "5px"); 
     /* Added Lines */ 
     div.css("pointer-events", "auto"); //Just in case, but it's probably not necessary 
     div.css("cursor", "pointer"); //It's nice to have 
     div.on('click', function() { //This is a click event delgated to the div 
      // Whatever code is needed when div is clicked 
     }); 
     //console.log(colorInfo); 
    }); 
} 

$(document).ready(function() { 
    var url = 'myapi.json'; 
    $.get(url, onDataReceived); 
}); 
+0

如果該框未取消選中以執行某些代碼,是否還有一種方法? –

+0

實際上,使用''('whatever_selector')。on('change',function(){....'或...'on('click',function(){...'可能需要熟悉'.checked'看到這篇文章:http://stackoverflow.com/a/7031408/2813224 – zer00ne

+0

div.on('click',function(){//這是一個點擊事件分配給div.css(「background-color」,「#333」); }); div.on('change',function(){ div.css(「background-color」,「#fff 「); });像那樣它不工作? –