activeIndex
仅在自动完成下拉菜单打开并且用户使用键盘上/下按钮选择选项时使用。
为了找出所选项目,您可以从
onAutocomplete
的第一个参数中读取。
演示:
document.addEventListener('DOMContentLoaded', function() {
var elem = document.querySelector('[id="vincularSetor.colaborador_id.autocomplete"]');
M.Autocomplete.init(elem, {
minLength: 0,
data: [{
id: 3,
text: "Name 1"
},
{
id: 4,
text: "Name 2"
}
],
onAutocomplete: (selectedItems) => {
var autocomplete = M.Autocomplete.getInstance(document.getElementById("vincularSetor.colaborador_id.autocomplete"));
console.log(selectedItems);
}
});
});
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@materializecss/[email protected]/dist/css/materialize.min.css">
<script src="https://cdn.jsdelivr.net/npm/@materializecss/[email protected]/dist/js/materialize.min.js"></script>
<div class="input-field outlined s4" style="margin: 5px 5px;">
<i class="material-icons prefix">search</i>
<input type="text" id="vincularSetor.colaborador_id.autocomplete" form="form_vincularSetor" class="autocomplete">
<label for="vincularSetor.colaborador_id.autocomplete">Nome do Colaborador</label>
</div>