我正嘗試使用具有多個選擇的自動填充文本框。但是,我遇到了jQueryUi的一個自動完成組件的一些麻煩。帶有自動填充建議的列表無法正確顯示。 從服務器我已經得到了價值這樣的:jQuery UI具有多選的自動完成功能在html頁面中不顯示數據
[{"id":"1","text":"Test1 [1001]"},{"id":"2","text":"Test2 [1002]"}]
但它不是在UI顯示。在UI是這樣的:
這裏是我的HTML代碼:
<label class="control-label">Group <span class="symbol required" aria-required="true"></span></label>
<input type="text" class="form-control" id="HSModels" tablename="ProductInfo" required />
<input type="hidden" class="form-control" id="HSModelsID" required />
我的腳本:
$("#HSModels")
.bind("keydown", function (event) {
if (event.keyCode === $.ui.keyCode.TAB &&
$(this).data("ui-autocomplete").menu.active) {
event.preventDefault();
}
})
.autocomplete({
minLength: 2,
source: function (request, response) {
$.getJSON("/api/Common/AutoCompleteListDataByTableName", { tableName: "ProductInfo", query: request.term },
response
);
},
search: function() {
// custom minLength
var term = extractLast(this.value);
if (term.length < 2) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function (event, ui) {
var HSModelsIDVal = $("#HSModelsID").val();
HSModelsIDVal += ", " + ui.item.id;
$("#HSModelsID").val(HSModelsIDVal)
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
return false;
}
//});
});
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
而在服務器端:
[HttpGet]
public HttpResponseMessage AutoCompleteListDataByTableName(string tableName, string query = "")
{
GenericRepository<DropdownListData> repository = new GenericRepository<DropdownListData>(_csmDb);
try
{
var parameters = new List<SqlParameter> { new SqlParameter("@TableName", tableName), new SqlParameter("@QueryText", query) };
List<DropdownListData> dataList = repository.ExecuteStoreProcedureToList("[GetAutoCompleteListDataByTableName]", parameters);
return Request.CreateResponse(HttpStatusCode.OK, dataList, RequestFormat.JsonFormaterString());
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.OK, new Confirmation { output = "error", msg = ex.Message }, RequestFormat.JsonFormaterString());
}
}
有什麼想法嗎?而且我還面臨另一個問題,即選擇一個後,它不會再次加載數據。
在螢火蟲:
第1次:GET http://localhost:40315/api/Common/AutoCompleteListDataByTableName?tableName=ProductInfo&query=te
和第2時間: GET http://localhost:40315/api/Common/AutoCompleteListDataByTableName?tableName=ProductInfo&query=%2C+te
我下面要做的事情like this
在此先感謝。
謝謝,它的工作完美:) – Metaphor
多一個查詢,如何刪除從隱藏字段的ID如果一個項目選擇後從文本框中刪除? – Metaphor
@Metaphor將需要相當數量的工作。考慮用某物包裝標籤並添加一個'x'按鈕。這樣,您可以獲取標籤的索引並從兩個數組中彈出該項目。 – Twisty