简体   繁体   English

自动完成,在退格和文本不可见之前不显示选项

[英]autocomplete, not showing options until backspace and text is not visible

ive created a JSfiddle here http://jsfiddle.net/8nzty/ code here:- 我在这里创建了一个JSfiddle http://jsfiddle.net/8nzty/代码: -

<script>
$(document).ready(function () {
    $(".items").click(function () {
        $(this).val('');
    });
    var items = [{ "ID": 1, "Name": "HP DL360p", "PartNo": " 670638-425", "Description": "" }, { "ID": 2, "Name": "Samsung 840 Pro 256GB", "PartNo": "", "Description": "256GB SSD" }, { "ID": 3, "Name": "HP MSA P2000", "PartNo": "AW568A", "Description": "" }, { "ID": 4, "Name": "HP BL460c G6", "PartNo": null, "Description": "HP G6 Blade" }]
    $("#Name").autocomplete({
        minLength: 0,
        source: items,
        focus: function (event, ui) {
            $("#Name").val(ui.item.Name);
            return false;
        },
        select: function (event, ui) {
            $("#Name").val(ui.item.Name);
            $("#PartNo").val(ui.item.PartNo);
            $("#Description").val(ui.item.Description);
            return false;
        }
    });
})
</script>
<table>
    <tbody>
        <tr>
            <td><span class="ui-helper-hidden-accessible" role="status" aria-live="polite">4 results are available, use up and down arrow keys to navigate.</span><input name="Name" class="items ui-autocomplete-input valid" id="Name" type="text" value="Item Name" autocomplete="off"></td>
            <td><input name="PartNo" class="items" id="PartNo" type="text" value="Part Number"></td>
            <td><input name="Description" class="items" id="Description" type="text" value="Description"></td>
            <td><input name="cmd" class="blue-button" id="btnAddItem" type="submit" value="+"></td>
            <td><input name="cmd" class="blue-button" id="btnRemoveItem" type="submit" value="-"></td>
        </tr>
    </tbody>
</table>

Whats supposed to happen: I type h and see a list of items beginning containing h I click an option part no and description are autofilled 应该发生什么:我输入h并看到一个开始包含h的项目列表我单击选项部分no并且描述是自动填充的

whats happening: i type h, nothing happens i hit backspace a dropdown appears with none visible text (maybe its white?) on mouseover i see text appear in the name box, click one and its autofilled as expected. 什么事情发生了:我输入h,没有任何反应我点击退格时出现一个没有可见文本的下拉列表(可能是白色?)鼠标悬停时我看到文本出现在名称框中,单击一个并按预期自动填充。

See Fiddle 小提琴

You almost got it. 你几乎得到了它。 The issue is that you don't provide a way to show the options on the screen. 问题是您没有提供在屏幕上显示选项的方法。 You're using an array of JSON objects, so the widget needs to be told what data and how to display it. 您正在使用JSON对象数组,因此需要告知窗口小部件有哪些数据以及如何显示它。

$("#Name").autocomplete({
  //existing widget settings
})
.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
  return $( "<li>" )
  .append( "<a>" + item.Name + "<br>" + item.Description + "</a>" )
  .appendTo( ul );
};

to the end of the autocomplete widget solves the issue. autocomplete小部件的末尾解决了问题。

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

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