2017-03-15 100 views
0

我在我的rails应用程序中使用jQuery-ui自动完成。当我输入一些输入时,我希望它自动选择自动填充框中的第一项。我应该如何实现这一点?jQuery-ui自动完成,选择第一项

jQuery(function() { 
    return $('#transaction_receiver_name').autocomplete({ 
     source: $('#transaction_receiver_name').data('autocomplete-source') 
    }); 
}); 

我的CSS

.ui-helper-hidden-accessible { 
    display: none; 
} 

ul.ui-autocomplete { 
    position: absolute; 
    list-style: none; 
    margin: 0; 
    padding: 0; 
    border: solid 1px #999; 
    cursor: default; 
    li { 
    background-color: #FFF; 
    color: black; 
    border-top: solid 1px #DDD; 
    margin: 0; 
    padding: 0; 
    a { 
     color: #000; 
     display: block; 
     padding: 3px; 
    } 
    a.ui-state-hover, a.ui-state-active { 
     background-color: #FFFCB2; 
    } 
    } 
} 

Input field

+0

为什么不makeuse了'open'回调的做到这一点。 – Twisty

+0

到目前为止您尝试过什么? – Twisty

回答

3

您只需要添加autoFocus: true,它会自动选择列表中显示的第一个元素。

jQuery(function() { 
    return $('#transaction_receiver_name').autocomplete({ 
     source: $('#transaction_receiver_name').data('autocomplete-source'), 
     autoFocus: true 
     } 

    }); 
}); 

下面是一个例子:

$(function() { 
 
    var availableTags = [ 
 
    "ActionScript", 
 
    "AppleScript", 
 
    "Asp", 
 
    "BASIC", 
 
    "C", 
 
    "C++", 
 
    "Clojure", 
 
    "COBOL", 
 
    "ColdFusion", 
 
    "Erlang", 
 
    "Fortran", 
 
    "Groovy", 
 
    "Haskell", 
 
    "Java", 
 
    "JavaScript", 
 
    "Lisp", 
 
    "Perl", 
 
    "PHP", 
 
    "Python", 
 
    "Ruby", 
 
    "Scala", 
 
    "Scheme" 
 
    ]; 
 
    $("#tags").autocomplete({ 
 
    source: availableTags, 
 
    autoFocus: true, 
 
    focus: function(event, ui) { 
 
     event.preventDefault(); 
 
     //Here you can add anycode you want to be executed when an item of the box is selected 
 
    }, 
 
    select: function(event, ui) { 
 
     event.preventDefault(); 
 
    //Code here will be executed when an item is clicked on 
 
    } 
 
    }); 
 
});
/* this will change the style of the selected element of the box*/ 
 

 
.ui-autocomplete .ui-menu-item .ui-state-active { 
 
    color: blue; 
 
    background: red; 
 
}
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 

 
<input id="tags">

+0

不确定是否正确:*如果设置为“true”,则在显示菜单时,第一项会自动对焦。*这会将焦点设置在第一项上,但不会选中它并关闭菜单。 – Twisty

+0

@Twisty我编辑了我的答案,我希望它清楚:) – Chiller

+0

@Chiller这个想法对我来说很好,除了'focus'部分。 这是因为'$(this).autocomplete(“close”);'在看到任何东西之前关闭了盒子,$(this).val(ui.item.key);'删除了文本域 – KSHMR

1

菜单打开时,你可以收集第一个列表项,并使用它作为价值。例如:

jQuery(function() { 
    return $('#transaction_receiver_name').autocomplete({ 
    source: $('#transaction_receiver_name').data('autocomplete-source'), 
    open: function(e, ui){ 
     var first = $(".ui-menu-item:eq(0) div").html(); 
     $(this).val(first); 
     return false; 
    } 
    }); 
}); 

这是未经测试的。

另一种方法是触发点击第一个元素。

jQuery(function() { 
    return $('#transaction_receiver_name').autocomplete({ 
    source: $('#transaction_receiver_name').data('autocomplete-source'), 
    open: function(e, ui){ 
     $(".ui-menu-item:eq(0)").trigger("click"); 
     return false; 
    } 
    }); 
}); 
+0

第一种方法会在输入第一个字母时立即在列表中添加第一个项目,如果您在半途删除该单词,它将自动完成一次。第二种方法不显示项目列表。它只会像第一种方法一样在列表中添加第一项。 – KSHMR

+0

如果项目列表已经可见,第二种方法适用于我。 –