Javascript Jquery-使用grep搜索表的所有列?

Javascript Jquery-使用grep搜索表的所有列?,javascript,jquery,html,Javascript,Jquery,Html,我开始学习Jquery,我试图搜索一个名为questions的表的所有列。目前它只搜索“q_text”列,但是我想扩展它的功能,以检查q_options_1、q_options_2、q_options_3和q_options_4。我试过使用| |运算符,但我不确定我是否把它放在了正确的位置 以下是相关代码: //Adding the search functionality to our search button $('#search').on('click', findMat

我开始学习Jquery,我试图搜索一个名为questions的表的所有列。目前它只搜索“q_text”列,但是我想扩展它的功能,以检查q_options_1、q_options_2、q_options_3和q_options_4。我试过使用| |运算符,但我不确定我是否把它放在了正确的位置

以下是相关代码:

    //Adding the search functionality to our search button
    $('#search').on('click', findMatches);

    function findMatches(criteria){

    $("#questions tbody tr").remove(); 

    //Get the JSON data from our HTML and convert it to a JavaScript object
    //In the real world, this data will likely be retrieved  from the server via an AJAX request
    var questions = JSON.parse(document.getElementById('questions-json').innerHTML);

var results = $.grep(questions, function(x, i){
var index = x.q_text.indexOf($('#searchText').val()) 
return (index >= 0);

})

console.log(results);

 //Loop through the list array and create a table row for each item.
$.each(results, function(i, question) {
 var tblRow = '<tr>' +
'<td>' + question.id + '</td>' +
'<td>' + question.q_text + '</td>' +
'<td>' + question.q_options_1 + '</td>' +
'<td>' + question.q_options_2 + '</td>' +
'<td>' + question.q_options_3 + '</td>' +
'<td>' + question.q_options_4 + '</td>' +
'<td>' + question.q_correct_option + '</td>' +
'<td>' + question.q_difficulty_level + '</td>' +
'</tr>'
//Add our table row to the 'questions' <table>
$(tblRow).appendTo('#questions tbody');
});
}
//将搜索功能添加到搜索按钮
$(“#搜索”)。在('单击',查找匹配项);
功能查找匹配(标准){
$(“#问题体tr”).remove();
//从HTML中获取JSON数据并将其转换为JavaScript对象
//在现实世界中,这些数据可能会通过AJAX请求从服务器检索
var questions=JSON.parse(document.getElementById('questions-JSON').innerHTML);
var results=$.grep(问题,函数(x,i){
var index=x.q_text.indexOf($('#searchText').val())
返回(索引>=0);
})
控制台日志(结果);
//循环遍历列表数组,并为每个项创建一个表行。
$。每个(结果、功能(i、问题){
变量tblRow=“”+
''+question.id+''+
''+问题.q_文本+''+
''+question.q_options_1+''+
''+question.q_options_2+''+
''+question.q_options_3+''+
''+question.q_options_4+''+
''+question.q\u更正选项+''+
''+问题.问题难度等级+''+
''
//将我们的表格行添加到“问题”中
$(tblRow)。附加到(“#问题tbody”);
});
}

您可以使用
$。每个
grep
中循环对象属性并测试每个属性

建议首先缓存
$('#searchText').val()的值,这样每次迭代都不需要dom搜索

var query = $('#searchText').val();
var results = $.grep(questions, function(x, i){
    var isMatch = false; 
     $.each(x, function(key, value){
        if( value.indexOf( query)>-1){
           isMatch = true;
           // break loop when match found
           return false;
        }
     });
     return isMatch;
});

您可以使用
$。每个
grep
中循环对象属性并测试每个属性

建议首先缓存
$('#searchText').val()的值,这样每次迭代都不需要dom搜索

var query = $('#searchText').val();
var results = $.grep(questions, function(x, i){
    var isMatch = false; 
     $.each(x, function(key, value){
        if( value.indexOf( query)>-1){
           isMatch = true;
           // break loop when match found
           return false;
        }
     });
     return isMatch;
});

实际发生的情况以及
结果是什么样子的?还提供
问题的示例。在jsfiddle.net或其他沙盒中的一个演示会有所帮助,这就是它目前的样子:本质上它是有效的,我只想扩展它,以便grep也可以搜索其他字段,即如果用户在搜索中键入“Dutch”,第一个条目仍然会出现。然后需要循环对象中的所有属性,并测试
grep
callback中的每个值实际发生了什么,以及
结果是什么样子的?还提供
问题的示例。在jsfiddle.net或其他沙盒中的一个演示会有所帮助,这就是它目前的样子:本质上它是有效的,我只想扩展它,以便grep也可以搜索其他字段,即如果用户在搜索中键入“Dutch”,第一个条目仍然会出现。然后需要循环对象中的所有属性,并测试
grep
callbackWorks中的每个值。非常好,谢谢!缓存值是我没有考虑的事情,我正在尝试找出如何循环对象属性,再次感谢!很好,谢谢!缓存值是我没有考虑的事情,我正在尝试找出如何循环对象属性,再次感谢!