在jquery php

时间:2016-01-14 05:14:46

标签: javascript php jquery html css

我有一个带有类名bus的div在foreach循环php中重复

<div class="bus" id="result1">
  <div class="seat red" data-number="A4(W)" data-book="1" amount="505" uniq-id="1452746805">
  </div>
  <div class="seat red" data-number="B4(W)" data-book="1" amount="505" uniq-id="1452746801">
  </div>
  <div class="seat red" data-number="C4(W)" data-book="1" amount="505" uniq-id="1452746823">
  </div>
  <div class="seat red" data-number="D4(W)" data-book="1" amount="505" uniq-id="1452746839">
  </div>
</div>

<div class="bus" id="result2">
  <div class="seat red" data-number="A4(W)" data-book="1" amount="501" uniq-id="1452746803">
  </div>
  <div class="seat red" data-number="B4(W)" data-book="1" amount="505" uniq-id="1452746802">
  </div>
  <div class="seat red" data-number="C4(W)" data-book="1" amount="505" uniq-id="1452746804">
  </div>
  <div class="seat red" data-number="D4(W)" data-book="1" amount="505" uniq-id="1452746801">
  </div>
</div>
  .....
  .....
  <div class="bus" id="resultn">
     .....
     ....
  </div>

点击带有课程seat的div时,会向其添加课程green 我想用类green计算每个div中的bus个类。 这是我的代码

$(document).on("click",".seat",function(){
   var total_class = $('.green').length;
})

问题是我从班级green的所有div获得bus班级总数

更新

当点击带有类座位的div时,我想将data-number属性值分别放入每个类总线的数组中,稍后它将用于某些数据库操作。

这是我的代码

$(document).on("click",".seat",function(){

  var temp=[];
  var seat_no = $(this).attr("data-number");
  temp.push(seat_no);

}

问题是我从所有具有类总线的div中获取所有数据编号属性

6 个答案:

答案 0 :(得分:1)

使用parent()方法访问您单击的元素上的父元素,这样您就可以获得类的实例数.green

var total_class = 0;    

$(document).on("click",".seat",function(){
    total_class = $(this).parent().children('.green').length
})

答案 1 :(得分:1)

试试这个:

在这种情况下

  1. 您需要选择单击的元素的父元素 你案件中的.bus
  2. 来自父母的
  3. 发现其孩子的班级为.green并获得其计数
  4. 这就是我在这段代码中所做的:

    var total_class = $(this).parent('.bus').children('.green').length;
    

    <强>样本:

        $(document).on("click", ".seat", function() {
          $(this).addClass('green');
          var total_class = $(this).parent('.bus').children('.green').length;
          alert(total_class);
        })
    .red {
      color: red
    }
    .green {
      color: green
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div class="bus" id="result1">
      <div class="seat red" data-number="A4(W)" data-book="1" amount="505" uniq-id="1452746805">b1
      </div>
      <div class="seat red" data-number="B4(W)" data-book="1" amount="505" uniq-id="1452746801">b1
      </div>
      <div class="seat red" data-number="C4(W)" data-book="1" amount="505" uniq-id="1452746823">b1
      </div>
      <div class="seat red" data-number="D4(W)" data-book="1" amount="505" uniq-id="1452746839">b1
      </div>
    </div>
    <hr>
    <div class="bus" id="result2">
      <div class="seat red" data-number="A4(W)" data-book="1" amount="501" uniq-id="1452746803">b2
      </div>
      <div class="seat red" data-number="B4(W)" data-book="1" amount="505" uniq-id="1452746802">b2
      </div>
      <div class="seat red" data-number="C4(W)" data-book="1" amount="505" uniq-id="1452746804">b2
      </div>
      <div class="seat red" data-number="D4(W)" data-book="1" amount="505" uniq-id="1452746801">b2
      </div>
    </div>

答案 2 :(得分:0)

$(document).on("click",".seat",function(){
   var total_class = $(this).closest(".bus").find('.green').length;
})

这将为您提供在同一总线中具有绿色等级的总div:

答案 3 :(得分:0)

试试这个:

 $(document).on("click",".seat",function(){
    var total_class = $(this).parent().find('.green').length;
 });

答案 4 :(得分:0)

$(document).ready(function () {
           $('.seat').click(function () {
           $(this).addClass('green');
          // $('#total').val($('.bus').find('div.green').length);

           szTotal = $('.bus').find('div.green').length;

           szResult = "Total no of green class in current div (id=" + $(this).closest('div.bus').attr('id') + ") = " + $(this).closest('div.bus').find('div.green').length

           szResult = szResult + "<br>" + "Total green class in All Div=" + szTotal

           alert(szResult)
       });
   });

答案 5 :(得分:0)

$(document).ready(function(){
    $('.seat').click(function(){
        $(this).parent().addClass('green');
        get_number_with_class('green');
    });
});

function get_number_with_class(classname)
{
    var total_class = $('div.'+classname).length;
}

对我有用