[英]How to get id of selected row in autocomplete JQuery UI
我想在我的autocomplete
功能中獲取所選項目的行ID,我從php
變量獲取源代碼的值。
<script>
$(function() {
var availableTags = [ <? php echo($toto); ?> ];
$("#foo").autocomplete({
source: availableTags
});
});
</script>
我已經嘗試過此功能,但它似乎不起作用。 實際上,當我將其添加到腳本中時,我的autocomplete
將不再起作用。
<script>
$(function () {
var availableTags = [ <? php echo($ListeNomsFormateeFinale); ?> ];
$("#nomClient").autocomplete({
source: availableTags
select: function (event, ui) {
$("#textfield1").val(ui.item.label); // display the selected text
$("#textfield2").val(ui.item.value); // display selected id
return false;
}
});
});
</script>
我在這里做錯了什么? 有解決這個問題的快速方法嗎?
編輯:
實際上,我實際上需要在source: availableTags
之后添加一個逗號,還刪除了return false
。 但它沒有返回所選行的ID,它實際上在兩個文本textfield1
和textfield2
寫入了相同的值
數組:數組可用於本地數據。 支持兩種格式:
字符串數組:[“ Choice1”,“ Choice2”]
具有標簽和值屬性的對象數組:[{標簽:“ Choice1”,值:“ value1”},...]標簽屬性顯示在建議菜單中。 當用戶選擇一個項目時,該值將插入到輸入元素中。 如果僅指定一個屬性,則將同時使用這兩個屬性,例如,如果僅提供值屬性,則該值也將用作標簽。
第二點(2.)提供了一種存儲所選對象的“ id”(或其他方式)的方法。 例如:
var yourSource = [ { label: "Choice1", id: "id1" } ];
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" /> <form> <input type="text" id="fooInput" /> </form> <div id="result"> </div> <script type="text/javascript"> var yourSource = [ { label: "A", id: "id1" }, { label: "B", id: "id2" }]; $("#fooInput").autocomplete({ source: yourSource, select: function(event, ui) { var e = ui.item; var result = "<p>label : " + e.label + " - id : " + e.id + "</p>"; $("#result").append(result); } }); </script>
因此,嘗試像這樣格式化數據源: var source = [ {label:"Foo", id:"1"}, ... ];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.