Khi thêm tokenSeparators: [",", " "] người dùng có thói quen đánh dấu phẩy, hoặc phím space cái này có chức năng hỗ trợ đánh tag tự động
<!DOCTYPE html>
<head>
<title></title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" />
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/js/select2.min.js"></script>
<style type="text/css">
.flag {
width: 15px;
height: 15px;
margin-right: 5px;
}
</style>
</head>
<body>
<select id="tags" multiple="multiple" style="width: 400px;">
<option value="Kerry" selected="selected">Kerry</option>
<option value="Cork">Cork</option>
</select>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#tags').select2({
tags: ["one","two"],
data: ["Clare","Cork","South Dublin"],
tokenSeparators: [",", " "],
placeholder: "Add your tags here",
selectOnClose: true,
closeOnSelect: false
});
});
</script>
</body>
</html>