如何选择属性除以逗号

时间:2015-07-24 12:15:01

标签: jquery jquery-selectors

我有这个选择器:

<div class="filterMenu">
<label data-id="5"></label>
<label data-id="2"></label>
</div>

<div class="galeria">
<div data-categories="5,2"></div>

<div data-categories="2,6"></div>
</div>

这就是我正在尝试的方式

$('.filterMenu label').on('click', function () {
            var id = $(this).data('id');
            $('.galeria > div').hide();
            $('.galeria > div').each(function () {
                var gid = String($(this).data('categories'));
                var gidt = gid.split(',');
                for ( var i = 0; i < gidt.length; i++) {
                    if ( gidt[i] == gid ) {
                        $(this).show();
                    }
                }
            });
        });

但它不能正确过滤,

有什么想法吗?

5 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情:

 try {
        String temp=sName.replace(" ", "%20");
        URL js = new URL("https://sids.roundone.asia/suggest.json?data="+temp);
        URLConnection jc = js.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
        String line = reader.readLine();
        JSONObject jsonResponse = new JSONObject(line);
        JSONArray jsonArray = jsonResponse.getJSONArray("results");
        for(int i = 0; i < jsonResponse.length(); i++){
            JSONObject r = jsonArray.getJSONObject(i);
            ListData.add(new SuggestGetSet(jsonResponse.get(String.vlaueOf(iss)));
        }
}

答案 1 :(得分:2)

你的代码不应该是:

&#13;
&#13;
if (gidt[i] == id) {
  $(this).show();
}
&#13;
&#13;
&#13;

而不是:

&#13;
&#13;
if (gidt[i] == gid) {
  $(this).show();
}
&#13;
&#13;
&#13;

答案 2 :(得分:1)

Try this
var gid = $(this).attr('data-categories');

答案 3 :(得分:1)

您的ID检查存在拼写问题,但您可以将其简化为

var $galerias = $('.galeria > div');
$('.filterMenu label').on('click', function() {
  var id = $(this).data('id') + '';
  $galerias.each(function() {
    var $this = $(this);
    var gidt = ($this.data('categories') + '').split(',');
    $this.toggle(gidt.indexOf(id) > -1);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="filterMenu">
  <label data-id="5">5</label>
  <label data-id="2">2</label>
</div>
<hr />
<div class="galeria">
  <div data-categories="5,2">5,2</div>
  <div data-categories="2,6">2,6</div>
  <div data-categories="6">6</div>
  <div data-categories="5">5</div>
  <div data-categories="2">2</div>
  <div data-categories="2,5,6">2,5,6</div>
</div>

或者

var $galerias = $('.galeria > div');
$('.filterMenu label').on('click', function() {
  var id = $(this).data('id');
  $galerias.each(function() {
    var $this = $(this);
    var gidt = $this.data('categories');
    $this.toggle(gidt.indexOf(id) > -1);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="filterMenu">
  <label data-id="5">5</label>
  <label data-id="2">2</label>
</div>
<hr />
<div class="galeria">
  <div data-categories="[5,2]">5,2</div>
  <div data-categories="[2,6]">2,6</div>
  <div data-categories="[6]">6</div>
  <div data-categories="[5]">5</div>
  <div data-categories="[2]">2</div>
  <div data-categories="[2,5,6]">2,5,6</div>
</div>

答案 4 :(得分:1)

你可以让你的代码更容易,因为@jai说我试过这样的

var foo = someSwiftString as NSString