根据类别

时间:2018-07-02 20:27:50

标签: javascript jquery html

我无法获取此代码来报告每个子代td带有“ up_avail”类的机器名称。该代码只是使用相同的计算机名称两次答复,而不是获取想要的两个计算机名称。

感谢您的帮助。

<table border=1>
   <th>Machine Name</th><th>Result</th><th>Status</th>
   <tr>
     <td class="tdresult" id="WN-MN161FY0X066">WN-MN161FY0X066</td>
     <td>Found</td>
     <td class='up_avail'>New</td>
   </tr>
   <tr>
     <td class="tdresult" id="WD-ORA60YY1U015">WD-ORA60YY1U015</td>
     <td>Found</td>
     <td class='up_success'>Complete</td>
   </tr>
   <tr>
     <td class="tdresult" id="WD-ORA60YY1U030">WD-ORA60YY1U030</td>
     <td>Found</td>
     <td class='up_avail'>New</td>
   </tr>
</table>

<div id="set_status" class=button>Check</div>

jQuery:

$('#set_status').click(function() {
  var arrayOfIds = $.map($(".up_avail"), function() {
     return $('.up_avail').parent().parent().parent().find('.tdresult').attr("id");
  });
  alert(arrayOfIds);
});

提琴

https://jsfiddle.net/ge0yn8uc/7/

3 个答案:

答案 0 :(得分:1)

简单地使用.siblings('.tdresult')映射所有所需元素会容易得多。从这里,您可以简单地循环讨论有问题的元素:

$('#set_status').click(function() {
  var arrayOfIds = $.map($(".up_avail"), function() {
    return $('.up_avail').siblings('.tdresult');
  });

  $.each(arrayOfIds, function(index, value) {
    console.log(arrayOfIds[0][index].id);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table border=1>
  <th>Machine Name</th>
  <th>Result</th>
  <th>Status</th>
  <tr>
    <td class="tdresult" id="WN-MN161FY0X066">WN-MN161FY0X066</td>
    <td>Found</td>
    <td class='up_avail'>New</td>
  </tr>
  <tr>
    <td class="tdresult" id="WD-ORA60YY1U015">WD-ORA60YY1U015</td>
    <td>Found</td>
    <td class='up_success'>Complete</td>
  </tr>
  <tr>
    <td class="tdresult" id="WD-ORA60YY1U030">WD-ORA60YY1U030</td>
    <td>Found</td>
    <td class='up_avail'>New</td>
  </tr>
</table>

<div id="set_status" class=button>Check</div>

答案 1 :(得分:1)

您需要使用>>> df.printSchema() root |-- id: integer (nullable = true) |-- name: string (nullable = true) |-- testing: string (nullable = true) |-- avg_result: string (nullable = true) |-- score: string (nullable = true) |-- active: string (nullable = true) >>> df.show() +---+----+-------+----------+-----+------+ | id|name|testing|avg_result|score|active| +---+----+-------+----------+-----+------+ | 1| sam| null| null| null| True| | 2| Ram| True| 0.05| 10| False| | 3| Ian| False| 0.01| 1| False| | 4| Jim| False| 1.2| 3| True| +---+----+-------+----------+-----+------+ 循环浏览或在up_avail类上使用map并以这种方式返回ID。

.each
$('#set_status').click(function() {
   var arrayOfIds = $(".up_avail").map(function() { return $(this).parent().find('.tdresult').attr('id'); }).get();
   
   alert(arrayOfIds);
 });
.button {
  width: 100px;
  text-align: center;
  background: green;
  color: #fff;
  padding: 5px;
  margin: 10px
}

答案 2 :(得分:0)

有一种更简单的方法,通过使用jQ的.siblings().get()和JS的.map()

$('#set_status').click(function() {

   var arrayOfIds = $(".up_avail").siblings(".tdresult").get().map(function(el) {
      return el.id;
   });
   
   alert(arrayOfIds);

 });
.button {
  width: 100px;
  text-align: center;
  background: green;
  color: #fff;
  padding: 5px;
  margin: 10px
}
<table border=1>
  <tr>
    <td class="tdresult" id="WN-MN161FY0X066">WN-MN161FY0X066</td>
    <td>Found</td>
    <td class='up_avail'>New</td>
  </tr>
  <tr>
    <td class="tdresult" id="WD-ORA60YY1U015">WD-ORA60YY1U015</td>
    <td>Found</td>
    <td class='up_success'>Complete</td>
  </tr>
  <tr>
    <td class="tdresult" id="WD-ORA60YY1U030">WD-ORA60YY1U030</td>
    <td>Found</td>
    <td class='up_avail'>New</td>
  </tr>
</table>

<button id="set_status" class="button">Check</button>

<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

或者使用ES6风格,利用箭头功能隐式返回

$('#set_status').click(function() {

  var arrayOfIds = $(".up_avail").siblings(".tdresult").get().map(el => el.id);

  alert(arrayOfIds);

});