循环 jQuery 中的表元素遍历特定类的所有输入字段



在这里,我想循环表的每个输入字段,并过滤掉具有特定输入字段类的字段。我知道如何在表的所有输入字段中循环,即:

$('#table_id :input').each(function(key) {
}

现在我需要知道如何使用特定的 if 进一步循环这些输入字段。让该类为".input-class"

这是一个简单的例子。您只想使用该类来获取具有类input-class<input>元素并将其推送到数组。

下面的代码将只返回具有类的行:input-class

//create inputs array
var inputValues = [];
//for each table row
$("#table_id tr").each(function()
{
  //get input value
  var inputValue = $(this).find(".input-class").val();
  
  //if not empty push to array
  if(inputValue !='undefined' && inputValue !=null )
    inputValues.push(inputValue);
});
//output all input values stored in array
console.log("All Filtered Rows:");
console.log(inputValues);
.dont-filter{
  background: white;
}
.input-class{
  background: red;
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table_id">
  <tr><th>row id</th><th>column 1</th><th>column 2</th></tr>
  <tr>
    <td><input class="input-class" value="row id 1"> 1
</td><td>row 1</td><td>row 1</td>
  </tr>
  <tr>
    <td><input class="input-class" value="row id 2"> 2
</td><td>row 2</td><td>row 2</td>
  </tr>
  <tr>
    <td><input class="dont-filter" value="row id 3"> 3
</td><td>row 3</td><td>row 3</td>
  </tr>
</table>

最新更新