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

javascript - How do I count the number of hidden divs of a certain class with jquery - Stack Overflow

programmeradmin1浏览0评论

I have a dynamic form that I've written in rails. I want to be sure that a user can add no more than five links.

I start with two links and I have another link that allows the user to add another field. I also have a link next to the links that allows the user to remove a field, which sets a hidden field and then hides the field with slideUp();.

I want to know if there are 5 fields on the screen that the user is hoping to submit.

Here's what I'm currently using - this just counts all of the divs with that classname.

if($(".classname").length <5){
//create element dynamically
}

I want to check if "style='display: none;'" How might I do that?

I have a dynamic form that I've written in rails. I want to be sure that a user can add no more than five links.

I start with two links and I have another link that allows the user to add another field. I also have a link next to the links that allows the user to remove a field, which sets a hidden field and then hides the field with slideUp();.

I want to know if there are 5 fields on the screen that the user is hoping to submit.

Here's what I'm currently using - this just counts all of the divs with that classname.

if($(".classname").length <5){
//create element dynamically
}

I want to check if "style='display: none;'" How might I do that?

Share Improve this question edited Jun 3, 2011 at 9:15 Arun P Johny 388k68 gold badges531 silver badges532 bronze badges asked Jun 3, 2011 at 9:05 CyrusCyrus 3,7175 gold badges36 silver badges68 bronze badges 1
  • 1 possible duplicate of Jquery count number of hidden elements within div – Felix Kling Commented Jun 3, 2011 at 9:09
Add a ment  | 

2 Answers 2

Reset to default 10

Use the :hidden selector:

if ($(".classname:hidden").length < 5) {
    //create element dynamically
}

This will return any element with that class which is not viewable to the user. If you just want to check for display:none, then use filter():

$(".classname").filter(function () {
    return $(this).css("display") == "none";
});

You can try like this

$('.classname:not([style*="display: none"])').length
发布评论

评论列表(0)

  1. 暂无评论