最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery selector for attributes with values greater than or less than an amount using > or < - Stack O

programmeradmin1浏览0评论

I have a few div elements like:

<div class="roomnamecta" data-price="1189" data-adult="3">Room 1</div>
<div class="roomnamecta" data-price="578" data-adult="1">Room 2</div>
<div class="roomnamecta" data-price="650" data-adult="2">Room 3</div>

In jQuery I'm able for example to display div which data-adult= a specific value

// init (first I'm hiding all my divs)
$('.roomnamecta').hide();
// now I'm just showing depending on "data-adult" value
$('.roomnamecta[data-adult="3"]').show();

What I would like to do is something like this:

$('.roomnamecta[data-adult>="3"]').show();
// doesn't work

And better what I want to accomplish is to do for example:

$('.roomnamecta[data-adult>="3"],.roomnamecta[data-price>="1100"]').show();

How to write such a query, do I have to use object? How?

I have a few div elements like:

<div class="roomnamecta" data-price="1189" data-adult="3">Room 1</div>
<div class="roomnamecta" data-price="578" data-adult="1">Room 2</div>
<div class="roomnamecta" data-price="650" data-adult="2">Room 3</div>

In jQuery I'm able for example to display div which data-adult= a specific value

// init (first I'm hiding all my divs)
$('.roomnamecta').hide();
// now I'm just showing depending on "data-adult" value
$('.roomnamecta[data-adult="3"]').show();

What I would like to do is something like this:

$('.roomnamecta[data-adult>="3"]').show();
// doesn't work

And better what I want to accomplish is to do for example:

$('.roomnamecta[data-adult>="3"],.roomnamecta[data-price>="1100"]').show();

How to write such a query, do I have to use object? How?

Share Improve this question edited Aug 12, 2020 at 4:20 Jason Aller 3,65228 gold badges41 silver badges39 bronze badges asked Mar 4, 2015 at 18:32 user367864user367864 2813 gold badges5 silver badges11 bronze badges 1
  • Possible duplicate of jQuery: Selecting all elements where attribute is greater than a value – Jason C Commented Jul 18, 2016 at 20:41
Add a comment  | 

1 Answer 1

Reset to default 18

Since you can't accomplish this with an attribute selector (like you're trying to do), you would have to iterate over the elements and check.

For instance, you could use the .filter() method to return the elements whose data-adult attribute value is greater than or equal to 3:

Example Here

$('.roomnamecta[data-adult]').filter(function () {
    return $(this).data('adult') >= 3;
}).show();

For your second query, you could use:

Example Here

$('.roomnamecta[data-adult], .roomnamecta[data-price]').filter(function () {
    return $(this).data('adult') >= 3 || $(this).data('price') >= 1100;
}).show();

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论