
// 获取目标input元素,其中udf_sline_6902修改为实际附加字段的API字段名称
var $input = jQuery('#for_udf_fields\\.udf_sline_6902');
// 创建下拉容器
var $dropdown = jQuery('<div class="autocomplete-dropdown"></div>')
.css({
'position': 'absolute',
'z-index': '1000',
'background': 'white',
'border': '1px solid #ddd',
'max-height': '200px',
'overflow-y': 'auto',
'display': 'none'
})
.insertAfter($input);
// 输入事件处理(带防抖)
var timer;
$input.on('input', function() {
clearTimeout(timer);
timer = setTimeout(() => {
var searchText = jQuery(this).val().trim();
if (searchText.length >= 2) { // 至少2个字符才请求
fetchSuggestions(searchText);
} else {
$dropdown.hide().empty();
}
}, 300);
});
// 获取建议数据
function fetchSuggestions(query) {
//调用回调函数,其中URL修改为实际回调自定义函数的自定义函数URL
jQuery.ajax({
url:"/AppIntegrations?serviceName=callbackFunctions&api_name=get_user_list&auth_token=b44073e0c0313066e05cb8f5b4bbe29c237d4e77b6a2a9e7bed97c4ae3ed4842a71f607baad0a9762685d4469065bb385a0b1f7cef910e619d94e3ed9a94b30d85031457&PORTALID=1",
type:"POST",
async:false,
cache:false,
data:{"arguments":'{"param":"'+query+'"}'},
success(data){
dataJson = JSON.parse(data);
result= dataJson.output;
console.log(result);
renderDropdown(result.users);
}
});
}
// 渲染下拉选项
function renderDropdown(items) {
$dropdown.empty();
items.forEach(item => {
jQuery('<div class="select2-results-dept-0 select2-result select2-result-selectable"></div>')
.text(item['email_id'])
.css({ 'padding': '5px', 'cursor': 'pointer', 'width': '500px' })
.hover(
() => jQuery(this).css('background', '#f5f5f5'),
() => jQuery(this).css('background', 'white')
)
.click(() => {
$input.val(item['email_id']);
$dropdown.hide();
})
.appendTo($dropdown);
});
$dropdown.show();
}
// 点击外部隐藏下拉
jQuery(document).click(function(e) {
if (!jQuery(e.target).closest('.autocomplete-dropdown, #for_udf_fields\\.udf_sline_301').length) {
$dropdown.hide();
}
});