Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery搜索JSON_Jquery_Json_Search - Fatal编程技术网

使用jQuery搜索JSON

使用jQuery搜索JSON,jquery,json,search,Jquery,Json,Search,我试图使用jQuery解析和搜索我的json文件,我能够解析json文件,但不能搜索它 我的HTML 我的jQuery $(document).ready(function() { $.getJSON('myJson.json', function(data) { $.each(data, function(i, item) { $('<li />').html(item.name + '<br>' + item.deta

我试图使用jQuery解析和搜索我的json文件,我能够解析json文件,但不能搜索它

我的HTML

我的jQuery

$(document).ready(function() {
    $.getJSON('myJson.json', function(data) {
        $.each(data, function(i, item) {
            $('<li />').html(item.name + '<br>' + item.details + '<br>').appendTo('#results');
        }
    });
});​
$(文档).ready(函数(){
$.getJSON('myJson.json',函数(数据){
$。每个(数据、功能(i、项){
$('
  • ').html(item.name+'
    '+item.details+'
    ')).appendTo('results'); } }); });​
  • 上面的代码返回所有项目,但是我现在希望允许用户搜索json文件,因此我包含一个表单输入,创建一个onclick事件,并将输入传递给搜索,请参见下面的链接

    问题是输入变量没有被传递到seach? 请帮忙


    更新:将变量传递给正则表达式似乎是个问题


    已解决:
    if(item.name.search(newregexp(q,“i”))!=-1{


    感谢您的尝试,也很抱歉浪费了您的时间。

    您需要在表单提交后获取文本框的值。如果您有文本框,它将永远不会被设置,因为它在文档准备好时(可能在输入任何内容之前)获取值

    $(文档).ready(函数(){
    $(“#提交”)。单击(函数(){
    var q=$('#query').val();
    $.getJSON('myJson.json',函数(数据){
    //迭代器
    $。每个(数据、功能(i、项){
    //使用正则表达式搜索查询的结果
    if(item.name.search(new RegExp(/''+q+''/i))!=-1){
    $('
  • ').html(item.name+'
    '+item.details+'
    ')).appendTo('results'); } }); }); }); });
  • 正则表达式正在读取变量

    $('#submit').click(function() {
        var q = $('#query').val();
        $.getJSON('myJson.json', function(data) {
            // iterator
            $.each(data, function(i, item) {
                // search the results using regular expression for the query
                if (item.name.search(new RegExp(q, "i")) != -1) {
                    $('<li />').html(item.name + '<br>' + item.details + '<br>').appendTo('#results');
                }
            });
        });
    });
    
    $(“#提交”)。单击(函数(){
    var q=$('#query').val();
    $.getJSON('myJson.json',函数(数据){
    //迭代器
    $。每个(数据、功能(i、项){
    //使用正则表达式搜索查询的结果
    if(item.name.search(newregexp(q,“i”))!=-1){
    $('
  • ').html(item.name+'
    '+item.details+'
    ')).appendTo('results'); } }); }); });

  • 我几乎可以肯定
    RegExp(/'+q+''/I)
    没有做你认为的事。@在我的编辑过程中,我在格式化后从他的小提琴上复制了代码,但它与发布的原始代码不同。我已经还原了编辑。你是正确的,但是如果我使用
    RegExp(/q/I)
    ,RegExp假设“q”是要匹配的字符串。我想你会发现我编写的库做了你想要的:谢谢gpojd,但是我尝试了你的建议,变量没有被传递。这在你发布的文章中起到了作用(尽管由于其他问题,它没有使它走得更远)。
    $('#submit').click(function() {
        var q = $('#query').val();
        $.getJSON('myJson.json', function(data) {
            // iterator
            $.each(data, function(i, item) {
                // search the results using regular expression for the query
                if (item.name.search(new RegExp(q, "i")) != -1) {
                    $('<li />').html(item.name + '<br>' + item.details + '<br>').appendTo('#results');
                }
            });
        });
    });