如何将图像添加到javascript数组?

时间:2013-10-17 13:24:17

标签: javascript html

我有一个页面,其搜索栏根据单独的js文件中的数据自动完成。如果我输入一个字母,它会抛出以该字母开头或包含该字母的相关名称,如果我选择结果,则显示该名称附带的详细信息。我只需要知道如何将图像链接到该数据,因此在选择时会显示带有该名称信息的图片。

这是我的Javascript代码:

$(function(){

    var currencies = [
        { value: 'Murray Smith', data: 'AFN', foto: src='img/logo.jpg' },
        { value: 'Brown Church', data: 'ALL' ,foto: src='../img/logo.jpg'},
        { value: 'Jack Jones', data: 'DZD' ,foto: src='../img/logo.jpg'},
        { value: 'Ben Clark', data: 'EUR' ,foto: src='../img/logo.jpg'},
        { value: 'Pete White', data: 'AOA' ,foto: src='../img/logo.jpg'},
        { value: 'East Caribbean dollar', data: 'XCD' ,foto: src='../img/logo.jpg'},
    ];

    // setup autocomplete function pulling from currencies[] array
    $('#autocomplete').autocomplete({
        lookup: currencies,
        onSelect: function (suggestion) {
            var thehtml = '<strong>Currency Name:</strong> ' + suggestion.value + ' <br>      <strong>Symbol:   </strong> ' + suggestion.data + '<br> <strong>Profile Pic:</strong> ' +     suggestion.foto;
            $('#outputcontent').html(thehtml);
        }
    });
}); 

提前谢谢。

3 个答案:

答案 0 :(得分:2)

使用此代码:

var thehtml = '<strong>Currency Name:</strong> ' + suggestion.value 
             + ' <br>  <strong>Symbol:   </strong> ' + suggestion.data 
             + '<br> <strong>Profile Pic:</strong> <img ' + suggestion.foto +' />';
                                                  ^^^ use image tag here

你的foto应该是这样的:

foto: "src='img/logo.jpg'"
     ^^^ add the double quotation mark to make it correct

答案 1 :(得分:0)

将您的数组更新为有效将有助于:)

 var currencies = [
      { value: 'Murray Smith', data: 'AFN', foto: '<img src="img/logo.jpg">' },
      { value: 'Brown Church', data: 'ALL' ,foto: '<img src="img/logo.jpg">'},
      { value: 'Jack Jones', data: 'DZD' ,foto: '<img src="img/logo.jpg">'},
      { value: 'Ben Clark', data: 'EUR' ,foto: '<img src="img/logo.jpg">'},
      { value: 'Pete White', data: 'AOA' ,foto: '<img src="img/logo.jpg">'},
      { value: 'East Caribbean dollar', data: 'XCD' ,foto: '<img src="img/logo.jpg">'},
 ];

答案 2 :(得分:0)

AndréDion在评论中提到,你的语法无效。

您应该将对象的foto属性值更改为:

foto: 'path/to/my/img.ext'

并在HTML中,在调用该属性时,将其附加到图像标记内。

thehtml += '<img src="'+foto+'" alt="don't forget the blind!" />';

希望这有帮助。

- Sid