>我创建了文本框,该文本框基于另一个列表中的数据处理自动完成功能。如果用户没有选择任何项目或输入了错误的文本,我需要清除文本框。下面是我的代码
$.ajax({
url: "http://address/_vti_bin/lists.asmx",
type: "POST",
dataType: "xml",
data: soapEnv,
contentType: "text/xml; charset="utf-8"",
success: function (xmlResponse) {
var domElementArray = $("z\:row", xmlResponse);
var dataMap = domElementArray.map(function () {
return {
value: $(this).attr('ows_AirportCode'),
id: $(this).attr('ows_AirportCode')
};
});
var data = dataMap.get();
//Find the Sharepoint Portal Search Box (this is a poor selector, but it is not properly named by sharepoint, well it is but INamingContainer getrs in the way)
$("input[title='AirportCode Required Field']").autocomplete(
{
source: data,
miniLength: 3,
response: function (event, ui) {
// ui.content is the array that's about to be sent to the response callback.
if (ui.content.length == 0) {
$("#empty-message").text("No results found");
} else {
$("#empty-message").empty();
}
}
}
);
}
}); //.ajax
谢谢
您可以在自动完成时订阅更改事件,并将所选值与匹配项的列表值数组进行比较:
.autocomplete({
...
.change: function( event, ui ) {
var input = $("input[title='AirportCode Required Field']");
var selectedValue = input.val();
var valid = false;
$.each(data , function (i, item) {
if (item.value === selectedValue) valid = true;
});
if (valid == false){
// clear the input if not found
input.val('');
}
}
})