简体   繁体   English

jQuery:选择div,其中所有选择内部没有0值

[英]jQuery: select divs where all select inside does not have 0 value

I have the following html structure: 我有以下html结构:

<div class="subform_uploading" id="subform1">
    <select class="form_file">
        <option value="0">Select file</option>
        <option value="file1">file1</option>
        ...
        <option value="file10">file10</option>
    <select>
    <select class="form_title">
        <option value="0">Select Title</option>
        <option value="title1">title1</option>
        ...
        <option value="title10">title10</option>
    <select>
</div>
...
<div class="subform_uploading" id="subform5">
    <select class="form_file">
        <option value="0">Select file</option>
        <option value="file1">file1</option>
        ...
        <option value="file10">file10</option>
    <select>
    <select class="form_title">
        <option value="0">Select Title</option>
        <option value="title1">title1</option>
        ...
        <option value="title10">title10</option>
    <select>
</div>

What I like to get with jQuery is number of div elements with "subform_uploading" class where both select values are not equal to 0. 我想用jQuery获得的是带有“ subform_uploading”类的div元素数量,其中两个选择值都不等于0。

You just need to compare the number of <select> elements within each <div> against the number of <select> elements in each <div> where the value is not 0 : 你只需要比较的数量<select>中的每个元素<div>对数量<select>中的每个元素<div>当值不是0

var numDivs = $('div.subform_uploading').filter(function() {

  var $this = $(this), 
      $select = $this.find('select');

  return $select.length == $select.filter(function() { return this.value != 0 }).length; 

}).length;

Here's a fiddle 这是一个小提琴

Try a filter like 尝试类似的过滤器

$('button').click(function () {
    var $els = $('.subform_uploading').filter(function () {
        return $(this).find('select').filter(function () {
            return $(this).val() == 0
        }).length == 0;
    });
    console.log($els, $els.length)
})

Demo: Fiddle 演示: 小提琴

var count = 0;
$('.subform_uploading').each(function(i) {
    select1 = $(this).find('select').eq(0).val();
    select2 = $(this).find('select').eq(1).val();
    if(select1 != '0' && select2 != '0') {
        count++;
    }
});

I am not sure if there is a way using a one liner jquery selector. 我不确定是否有使用一个线性jquery选择器的方法。 THis function though will return what you wanted. 该函数虽然会返回您想要的。

Demo : JSFiddle 演示: JSFiddle

function getDivs () {
    var outDivs = [];
    var divs = $('div.subform_uploading');
    divs.each (function ( ) {
       var sels =   $(this).find ('select option[value!="0"]:selected');
       if (sels.length == 2) {
            outDivs.push ( $(this) );
       }
   });
   return outDivs;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM