简体   繁体   English

Coffee Script自动完成功能,带有jQuery,JSON和id变量

[英]Coffee Script Autocomplete with jQuery, JSON and id variable

I'm a bit stuck here and can't seem to get jQuery autocomplete to work for me. 我有点卡在这里,似乎无法让jQuery自动完成为我工作。 I'm trying to autocomplete from a JSON document pull. 我正在尝试从JSON文档提取中自动完成。

IE: IE浏览器:

http://REMOTE_HOST/names.json

Which returs something like: 哪个类似:

[{"label":"Brian House", "id" : 1},
{"label":"Joe Green", "id" : 2},
{"label":"Fisher Gennings", "id" : 3},
{"label":"Sheila Williams", "id" : 4},
{"label":"Brett Nelson", "id" : 5},
{"label":"Angie Katz", "id" : 6},
{"label":"Zoe Middleton", "id" : 7},
{"label":"Parker Jones", "id" : 9}]

What I want to do is have the label autocomplete in a text field (user_friend_name) then populate a hidden field (user_friend_name_id) with the id. 我想做的是在文本字段(user_friend_name)中具有标签自动完成功能,然后使用ID填充隐藏字段(user_friend_name_id)。

#user_friend_name
#user_friend_name_id

Coffee script I'm currently using. 我正在使用的咖啡脚本。

$(document).ready ->
        $('#user_friend_name').autocomplete
                source: "http://REMOTE_HOST/names.json"
                select: (event,ui) -> $("user_friend_name_id").val(ui.item.id)

Right now it looks like I get one JSON pull of all the names but no selection or filetering as you type. 现在看来,我在输入所有名称时都会得到一个JSON拉取,但键入时没有选择或过滤。 Using just a standard array ['foo', 'food', 'trees'] as the source seems to work fine. 仅使用标准数组['foo','food','trees']作为源似乎可以正常工作。

Assuming your data is being returned as you showed above, then ui.item.id is the correct property to access. 假设如上所示返回了数据,则ui.item.id是要访问的正确属性。

I made a fiddle of your example. 我把你的榜样弄得一团糟。 Try playing with console.log() and console.dir() to debug properties. 尝试使用console.log()console.dir()调试属性。

http://jsfiddle.net/fMWqU/ http://jsfiddle.net/fMWqU/

If you're still not having luck, then the problem may be your json document. 如果您仍然没有运气,那么问题可能出在您的json文档上。 Make sure the document is on the same domain as your script to prevent cross-origin errors. 确保文档与脚本位于同一域中,以防止跨域错误。 I've also noticed in the past that a json document with Windows line endings cause some browsers to fail. 我过去也注意到,带有Windows行尾的json文档会导致某些浏览器失败。 Try doing a regular $.ajax() and use console.dir() in the done() and fail() handlers. 尝试执行常规的$.ajax()并在done()和fail()处理$.ajax()使用console.dir()

xhr = $.ajax
  url : 'http://REMOTE_HOST/names.json'
  dataType : 'json'

xhr.done ->
  console.log arguments

xhr.fail ->
  console.log 'Fail! ', arguments

I believe they should have value not id properties. 我相信它们应该具有值而不是id属性。 I'm not familiar with coffee script but I'm sure this is legible: 我不熟悉咖啡脚本,但是我确定这是清晰的:

$.getJSON("http://REMOTE_HOST/names.json", function(response) {
    var newData = response.map(function(el) { return {label: el.label, value: el.id })
    $('#user_friend_name').autocomplete
        source: "http://REMOTE_HOST/names.json"
        select: (event,ui) -> $("user_friend_name_id").val(ui.item.value)
})

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

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