搜索超过1进入json

时间:2015-02-25 09:03:52

标签: javascript jquery json

我试图找出如何在JSON文件中深入搜索更深层次的内容。

其结构如下

json>data>movies>

在电影中,不同的电影被列举为0,1,2,3,4等,所以

json>data>movies>3>

将包含第4部电影的电影信息 每个包含键值对。我使用的代码似乎只是看起来很深,我似乎无法让它更深入。

代码如下

$('#search').keyup(function () { // i understand this to watch the form id search
    var searchField = $('#search').val(); // sets variable searchfield from typed content, .val grabs the content
    if (searchField.length) // this sets the length to false by default ensuring the code only runs when it is supposed to and not all the time
    {
        var myExp = new RegExp(searchField, "i"); // sets variable myexp to regular expression using "i" for case insensitive
        var found = 0; // sets variable found to 0 to keep div hidden until match is found
        $.getJSON('/secure/movies.json', function (data) { //gets movies.json file pushes to data function
            var output = '<ul class="searchresults">';
            $.each(data, function (key, val) { // this should make key pairs from data
                if (val.title.search(myExp) !== -1) { // this is the problem i think
                    console.log(val); // this should show our search term results in console
                    found = 1;
                    output += '<li>';
                    output += '<h2>' + val.title + '</h2>';
                    output += "<a href=" + val.image + ' target="_blank" ></a>';
                    output += '</li>';
                }
            });
            output += '</ul>';
            if (found == 1) {
                $('#update').removeClass('update-hidden');
                $('#update').html(output);
            } else {
                $('#update').addClass('update-hidden');
            }
        });
    } else {
        $('#update').addClass('update-hidden');
    }
});

我试过操纵val.title.search(myexp),但是我可能会错过这条船上的val.title.search。提前谢谢

0 个答案:

没有答案