2017-07-24 114 views
1

我使用以下mapbox.js example從GeoJSON的文件過濾功能。這個例子是不言自明 - 第一步是要查找被選中,並建立自己的值的列表所有複選框。附加多個值到一個DIV

目前,該複選框值都是單一字符串。我的問題很簡單,可以value在複選框是一個整數數組(或字符串數​​組?),如果是這樣,怎麼會在div input附加整數數組來value,這樣就可以通過多個值在一次篩選?

for (var i = 0; i < checkboxes.length; i++) { 
     if (checkboxes[i].checked) on.push(checkboxes[i].value); 

<div id='filters' class='ui-select'> 
    <div><input type='checkbox' checked=checked class='filter' 
      name='filter' id='fast-food' value='fast-food'/><label for='fast-food'>fast-food</label></div> 
    <div><input type='checkbox' checked=checked class='filter' 
      name='filter' id='bicycle' value='bicycle'/><label for='bicycle'>bicycle</label></div> 
    <div><input type='checkbox' checked=checked class='filter' 
      name='filter' id='bar' value='bar'/><label for='bar'>bar</label></div> 
</div> 
<div id='map'></div> 

<script> 
L.mapbox.accessToken = 'pk.eyJ1IjoiaXNrYW5kYXJibHVlIiwiYSI6ImNpazE3MTJldjAzYzZ1Nm0wdXZnMGU2MGMifQ.i3E1_b9QXJS8xXuPy3OTcg'; 
var map = L.mapbox.map('map', 'mapbox.dc-markers'); 
var filters = document.getElementById('filters'); 
var checkboxes = document.getElementsByClassName('filter'); 

function change() { 
    // Find all checkboxes that are checked and build a list of their values 
    var on = []; 
    for (var i = 0; i < checkboxes.length; i++) { 
     if (checkboxes[i].checked) on.push(checkboxes[i].value); 
    } 
    // The filter function takes a GeoJSON feature object 
    // and returns true to show it or false to hide it. 
    map.featureLayer.setFilter(function (f) { 
     // check each marker's symbol to see if its value is in the list 
     // of symbols that should be on, stored in the 'on' array 
     return on.indexOf(f.properties['marker-symbol']) !== -1; 
    }); 
    return false; 
} 

// When the form is touched, re-filter markers 
filters.onchange = change; 
// Initially filter the markers 
change(); 
</script> 

回答

1

你可以充分利用jQuery的.data() method,讓您的屬性名稱和值分配給在DOM元素。您可以在數據元素的值設置爲可解析爲它自己的整數數組的一個逗號分隔的列表。

一些(未測試的)僞代碼作爲一個例子:

<div id='filters' class='ui-select'> 
    <div><input type='checkbox' checked=checked class='filter' 
      name='filter' id='fast-food' value='fast-food' data-filter='fast-food,restaurants'/><label for='fast-food'>fast-food</label></div> 
    <div><input type='checkbox' checked=checked class='filter' 
      name='filter' id='bicycle' value='bicycle'/><label for='bicycle' data-filter='bicycle, transportation'>bicycle</label></div> 
    <div><input type='checkbox' checked=checked class='filter' 
      name='filter' id='bar' value='bar' data-filter='bar, restaurants'/><label for='bar'>bar</label></div> 
</div> 

... 

function change() { 
    // Find all checkboxes that are checked and build a list of their values 
    var on = []; 
    for (var i = 0; i < checkboxes.length; i++) { 
     if (checkboxes[i].checked) { 
      var array = $(checkboxes[i]).data("filter").split(','); 
      Array.prototype.push.apply(on,array); 
     } 
    } 

    ... 
}