自动填充搜索有效,但未被选中

时间:2013-11-14 15:40:10

标签: javascript jquery

输入文本框是通过jquery动态创建的。自动填充效果很好。但是当我选择一个项目时,它的值没有正确呈现,请问我在这里错过了哪里?

Jquery的

$(function() {
  var projects =  [
    {
        "label": "ajax",
        "value": "1003",
        "desc": "foriegn"
    },
    {
        "label": "jquery",
        "value": "1000",
        "desc": "dd"
    },


    {
        "label": "wordpress theme",
        "value": "1000",
        "desc": "h"
    },
    {
        "label": "xml",
        "value": "1000",
        "desc": "j"
    } ]; 

$("#addButton");
var counter = 13;

$("#addButton").click(function() {
    var newTextBoxDiv = $(document.createElement('div'))
            .attr("id", 'TextBoxDiv' + counter);

    var roleInput = $('<input/>', {
        type: 'text',
        placeholder: 'Role',
        name: 'Role' + counter,
        id: 'project-description' + counter
    });

    var searchInput = $('<input/>', {
        type: 'text',
        placeholder: 'search',
        name: 'search' + counter,
        id: 'project' + counter
    });

    var hidd = $('<input/>', {
        type: 'hidden',
        name: 'searchhid' + counter,
        id: 'project-id' + counter
    });


    newTextBoxDiv.append(roleInput).append(searchInput).append(hidd);
    newTextBoxDiv.appendTo("#TextBoxesGroup");
    $("#project" + counter).autocomplete({
        minLength: 0,
        source: projects,
        focus: function(event, ui) {
            $("#project" + counter).val(ui.item.label);
            return false;
        },
        select: function(event, ui) {
            $("#project" + counter).val(ui.item.label);
            $("#project-id" + counter).val(ui.item.value);
            $("#project-description" + counter).val(ui.item.value);
            $("#project-icon" + counter).attr("src", "images/" + ui.item.icon);
            return false;
        }
    })


    counter++;
});





});

` Html在

之下
<div id="project-label"></div>

<input id="project" />
<input type="hidden" id="project-id" />

<input type="text" disabled="disabled" id="project-description"></p>


 <div id='TextBoxesGroup'>
 <div id="TextBoxDiv1" class="form-inline control-group">

 </div>
</div>
 <input type='button' value='Add' id='addButton' />

请告诉我这条代码中缺少的是什么。

1 个答案:

答案 0 :(得分:0)

我认为这是一个JavaScript错误。自动填充中的select事件会查找$("#project-icon" + counter),但我看不到该元素在任何地方呈现。此外,在尝试设置该元素的src属性时,您引用ui.item.icon,但您的值数组不包含该属性。