简体   繁体   English

通过遍历JSON文件来填充数据列表选项

[英]Populate datalist options by iterating through JSON file

I'm trying to create a search box that will essentially autocomplete based on user input from a key-value pair in a json file. 我正在尝试创建一个搜索框,该框实际上将根据json文件中的键值对的用户输入自动完成。 It looked like using datalist might be the best option for this, but when I execute the code below, no option tags appear in the html. 看起来使用数据列表可能是最好的选择,但是当我执行下面的代码时,html中没有选项标签。

I am still pretty new to jquery and json, so I'm WAY open to suggestions. 我对jquery和json还是很陌生,所以我很乐意接受建议。

Here's the json. 这是json。 The list contains 1450 items if that's relevant: 如果相关,此列表包含1450个项目:

{ "osCars": [
{   "id": 1,
    "name": "Cadillac",
    "type": "Sedan",
    "desc": "A fine American automobile"
},
{   "id": 2,
    "name": "BWM",
    "type": "Sedan",
    "desc": "A fine German automobile"
},
{   "id": 3,
    "name": "Lexus",
    "type": "Sedan",
    "desc": "A fine Japanese automobile"
}
]}

Here's the html: 这是html:

<input type="text" maxlength="30" list="carList" id="carSearchBox" name="carSearchBox" pattern="[A-Za-z '-]*$" placeholder="search cars" autofocus autocomplete="on">
<datalist id="carList"></datalist>
<script src="js/main.js"></script>
<script>window.onload=getCars;</script>

And here's the javascript/jquery: 这是javascript / jquery:

function getCars() {
    var url, carOption;
    url = 'js/cars.json';

    $.getJSON(url, function(data) {
    //populate the cars datalist
        $(data.osCars).each(function() {
            carsOption = "<option value=\"" + this.id + "\">" + this.name + "</option>";
            $('#carList').append(carsOption);
        });
    });
}

It turns out the problem was not with my javascript as I suspected, but rather with my JSON file. 事实证明,问题出在我怀疑的不是我的JavaScript,而是我的JSON文件。 I used the URL of a different JSON file I have (that I know is valid) and everything worked fine. 我使用了我拥有的另一个JSON文件的URL(我知道这是有效的),并且一切正常。 The file I'd like to use is too long to validate with jsonLint, and has some lengthy text values. 我想使用的文件太长,无法通过jsonLint进行验证,并且具有一些冗长的文本值。 There's probably an errant double-quote mark in there somewhere that I didn't see, and that is throwing it off. 我可能没看到的地方可能有一个错误的双引号,这正在扔掉它。

I believe the problem is your use of this.id as the value, rather than this.name. 我相信问题是您使用this.id作为值,而不是this.name。 It appears that the datalist and autocomplete functionality in HTML5 is not implemented consistently across browsers, just yet. 看来,HTML5中的数据列表和自动完成功能尚未在所有浏览器中实现一致。 Ex: Chrome seems to have a problem when in a datalist, an option's inner html (display value) differs from the value. 例如:当在数据列表中,选项的内部html(显示值)与值不同时,Chrome似乎有问题。

Changing your code to use "this.name" for both id and value works fine (in Chrome at least): 将您的代码更改为将ID和值都使用“ this.name”可以正常工作(至少在Chrome中):

function getCars() {
    $(data.osCars).each(function (idx, o) {
        carsOption = "<option value='" + this.name + "'>" + this.name + "</option>";
        $('#carList').append(carsOption);
    });
}

See this fiddler example: http://jsfiddle.net/6HGuU/ 请参阅以下提琴手示例: http : //jsfiddle.net/6HGuU/

And this related question: HTML5 datalist value vs. inner-text 和这个相关的问题: HTML5数据列表值与内部文本

If you need to retrieve the selected ID and not the name, then you can look this up as a separate step (the related question contains an answer with a proposed approach for doing exactly this). 如果您需要检索选择的ID而不是名称,则可以将其作为一个单独的步骤查找(相关的问题包含一个答案,并提供了建议的方法来做到这一点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM