获取具有两个条件的所有文本框值

时间:2015-01-19 06:46:20

标签: javascript jquery

我有多个具有相同类和不同ID的文本框。我想为每个循环应用两个搜索条件。例如,我有这些文本字段: -

<input type="text" id="grid_5_collect_businame" class="search_ ui-autocomplete-input" style="width:100%;" value="" autocomplete="off">
<input type="text" id="grid_5_client_businame" class="search_" style="width:100%;" value="">
<input type="text" id="grid_5_collnurs" class="search_" style="width:100%;" value="">
<input type="text" id="grid_6_collect_businame" class="search_ ui-autocomplete-input" style="width:100%;" value="" autocomplete="off">
<input type="text" id="grid_6_client_businame" class="search_" style="width:100%;" value="">
<input type="text" id="grid_6_collnurs" class="search_" style="width:100%;" value="">

现在我要检索所有那些具有“search_”类的文本框值,这些id以“grid_5”开头,我是如何

2 个答案:

答案 0 :(得分:2)

您可以使用组合多种条件,例如

$('input.search_[id^=grid_5]')

答案 1 :(得分:0)

您需要使用正则表达式.match()检查ID,并使用.filter()过滤掉符合条件的ID。

工作代码段:

&#13;
&#13;
var filteredTextboxes = $("input.search_").filter(function(element){
  var id = this.getAttribute('id');
  if(id.match(/^grid_5/))
    return id;
});

console.dir(filteredTextboxes);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="grid_5_collect_businame" class="search_ ui-autocomplete-input" style="width:100%;" value="" autocomplete="off">
<input type="text" id="grid_5_client_businame" class="search_" style="width:100%;" value="">
<input type="text" id="grid_5_collnurs" class="search_" style="width:100%;" value="">
<input type="text" id="grid_6_collect_businame" class="search_ ui-autocomplete-input" style="width:100%;" value="" autocomplete="off">
<input type="text" id="grid_6_client_businame" class="search_" style="width:100%;" value="">
<input type="text" id="grid_6_collnurs" class="search_" style="width:100%;" value="">
&#13;
&#13;
&#13;

电文读出: