jQuery TagIt(自动完成)通过 AJAX 获取 JSON 列表

jsAjax

1个回答

写回答

德萨

2025-07-09 17:52

+ 关注

Ajax
Ajax

jQuery TagIt是一个用于实现自动完成功能的插件。它可以通过Ajax请求获取JSON列表,并将列表中的内容作为选项展示给用户。在用户输入时,TagIt会根据用户的输入过滤出匹配的选项,以供用户选择。下面是一段使用jQuery TagIt的案例代码:

html

<!DOCTYPE html>

<html>

<head>

<title>jQuery TagIt</title>

<link rel="stylesheet" type="text/CSS" class="url" target="_blank" rel="nofollow noreferrer" href="/to/?target=jquery.tagit.CSS">

<script src="https://code.jquery.com/jquery-3.6.0.min.JS"></script>

<script src="jquery.tagit.JS"></script>

<script>

$(document).ready(function() {

$('#tags').tagit({

autocomplete: {

source: function(request, response) {

$.Ajax({

url: 'example.JSon',

dataType: 'JSon',

data: {

term: request.term

},

success: function(data) {

response(data);

}

});

}

}

});

});

</script>

</head>

<body>

<input type="text" id="tags">

</body>

</html>

在上面的代码中,我们使用了标签引入了jQuery TagIt的样式表,并使用