我問了一個關於live searching a table的問題,但我有多個字段,如文本和選擇下拉字段。我寫了一個js代碼基於我從previous question得到了答案:從多個字段輸入合併搜索結果
$("#deviceName").keyup(function(){
var deviceNameFilter = $(this).val();
$("#device-list_table tbody tr").each(function(){
if ($(this).text().search(new RegExp(deviceNameFilter, "i")) < 0) {
$(this).hide();
} else {
$(this).show();
}
});
});
$("#broadcastProg").change(function(){
var broadcastProgFilter = $("#broadcastProg option:selected").text();
$("#device-list_table tbody tr").each(function(){
if ($(this).text().search(new RegExp(broadcastProgFilter, "i")) < 0) {
$(this).hide();
} else {
$(this).show();
}
});
});
$("#store").change(function(){
var storeFilter = $("#store option:selected").text();
$("#device-list_table tbody tr").each(function(){
if ($(this).text().search(new RegExp(storeFilter, "i")) < 0) {
$(this).hide();
} else {
$(this).show();
}
});
});
$("#deviceModel").change(function(){
var deviceModelFilter = $("#deviceModel option:selected").text();
$("#device-list_table tbody tr").each(function(){
if ($(this).text().search(new RegExp(deviceModelFilter, "i")) < 0) {
$(this).hide();
} else {
$(this).show();
}
});
});
$("#deviceStatus").change(function(){
var deviceStatusFilter = $("#deviceStatus option:selected").text();
$("#device-list_table tbody tr").each(function(){
if ($(this).text().search(new RegExp(deviceStatusFilter, "i")) < 0) {
$(this).hide();
} else {
$(this).show();
}
});
});
$("#logOutputLog").keyup(function(){
var logOutputLogFilter = $(this).val();
$("#device-list_table tbody tr").each(function(){
if ($(this).text().search(new RegExp(logOutputLogFilter, "i")) < 0) {
$(this).hide();
} else {
$(this).show();
}
});
});
例如,我在deviceName
領域,表更新輸入關鍵詞並顯示結果,但是當我選擇broadcastProg
下拉菜單欄中選擇一個選項,它也會顯示結果,但只會顯示選定選項,而不會考慮deviceName
字段的結果。所以應該是這樣的,例如當我在deviceName
文本字段中輸入「iPhone」並在broadcastProg
下拉字段中選擇「Schedule 1」時,那麼表格應該在設備名稱列下面顯示具有「iPhone」的行並且在下面顯示「Schedule 1」廣播節目專欄。那我該如何合併結果呢?
另外,deviceName
字段只應搜索設備名稱列,不包括其他列。就我而言,它會搜索所有列。
希望有人可以幫忙。
謝謝你的答案,但你是什麼意思,以避免合併?我想合併結果,而不是避免它。 –
對不起,它不能合併.. –
好的。謝謝。所以如果我的其他部分中的條件語句爲我的文本字段和下拉字段的其餘部分放置更多? –