将多个值附加到单个div

时间:2017-07-24 14:17:52

标签: javascript jquery html mapbox

我使用以下mapbox.js example来过滤geojson文件中的功能。该示例是不言自明的 - 第一步是找到所有选中的复选框并构建其值列表。

目前,复选框值都是单个字符串。我的问题很简单,复选框中的value可以是整数数组(或字符串数​​组吗?),如果是这样,如何在{{1}中将整数数组附加到value这样你就可以一次过滤多个值?

div input

1 个答案:

答案 0 :(得分: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);
        }
    }

    ...
}
相关问题