Tag-it 將 `
官網:Demo & Docs
<script src="jquery.min.js"></script>
<script src="jquery-ui.min.js"></script> <!-- 包含 core, widget, position, autocomplete, menu -->
<script src="tag-it.js"></script>
<link rel="stylesheet" href="jquery-ui.css" /> <!-- jQuery UI 主題 -->
<link rel="stylesheet" href="jquery.tagit.css" /> <!-- Tag-it 專用樣式 -->
<div id="container">
<p><b>範例 1:單一隱藏欄位同步值</b></p>
<ul id="myTags"></ul>
<input type="hidden" id="myTagsValues" />
<button id="submit1" type="button">獲取值</button>
<p><b>範例 2:預設標籤、允許空格、自定樣式</b></p>
<ul id="singleFieldTags"></ul>
<input name="tags" id="mySingleField" value="CSharp, jQuery" disabled="true" />
<button id="submit2" type="button">獲取值</button>
</div>
#container { width: 400px; }
input[type=button] { padding: 8px; }
/* 容器邊框 */
#singleFieldTags { border: 1px solid #b1c9dc; background: #e7e3ca; }
/* 輸入框 */
#singleFieldTags input { background: #e7e3ca; color: blue; }
/* 標籤樣式 */
#singleFieldTags li { background: #e7e3ca; border: 1px solid #930; color: red; }
/* 移除輸入框父容器邊框 */
#singleFieldTags .tagit-new { border: none; }
$(function() {
// 預設標籤庫 (自動完成來源)
var sampleTags = ['c++', 'java', 'php', 'coldfusion', 'javascript', 'asp', 'ruby', 'python', 'c', 'scala', 'groovy', 'haskell', 'perl', 'erlang', 'apl', 'cobol', 'go', 'lua'];
// 範例 1:單一隱藏欄位同步 (逗號分隔)
$('#myTags').tagit({
singleField: true, // 啟用單一欄位模式
singleFieldNode: $('#myTagsValues'), // 同步目標 input
availableTags: sampleTags, // 自動完成源
placeholderText: '輸入技術標籤...' // 輸入框佔位符
});
// 範例 2:允許標籤含空格、預設值、自定樣式
$('#singleFieldTags').tagit({
availableTags: sampleTags,
singleField: true,
allowSpaces: true, // 標籤內允許空格 (如 "c sharp")
singleFieldNode: $('#mySingleField'),
// 事件鉤子
beforeTagAdded: function(event, ui) {
console.log('即將添加:', ui.tagLabel);
// return false; // 可攔截
},
afterTagAdded: function(event, ui) {
console.log('已添加:', ui.tagLabel, '當前所有:', $('#mySingleField').val());
},
beforeTagRemoved: function(event, ui) {
console.log('即將移除:', ui.tagLabel);
},
afterTagRemoved: function(event, ui) {
console.log('已移除:', ui.tagLabel);
},
onTagClicked: function(event, ui) {
console.log('點擊標籤:', ui.tagLabel);
}
});
// 獲取值按鈕
$('#submit1').click(function() { alert($('#myTagsValues').val()); });
$('#submit2').click(function() { alert($('#mySingleField').val()); });
});
| 選項 | 類型 | 預設 | 說明 |
|---|---|---|---|
| singleField | Boolean | false | true=同步到單一 input (逗號分隔) |
| singleFieldNode | Selector/jQuery | null | 目標 input 元素 |
| singleFieldDelimiter | String | ',' | 分隔符 |
| allowSpaces | Boolean | false | true=標籤可含空格 (需 singleField) |
| availableTags | Array | [] | 自動完成資料源 |
| autocomplete | Object | {} | 傳遞給 jQuery UI Autocomplete 選項 |
| caseSensitive | Boolean | false | 標籤大小寫敏感 |
| removeConfirmation | Boolean | false | true=刪除需確認 |
| tagLimit | Number | null | 最大標籤數 |
| readOnly | Boolean | false | 只讀模式 |
| placeholderText | String | '' | 輸入框 placeholder |
Tag-it 產生結構:
<ul class="tagit">
<li class="tagit-choice">標籤文字 <a class="tagit-close">×</a></li>
<li class="tagit-new"><input class="tagit-input" type="text" /></li>
</ul>
覆蓋關鍵類別:
.tagit { border: 1px solid #ddd; padding: 4px; background: #fff; }
.tagit-choice { background: #e0e0e0; border-radius: 3px; padding: 2px 8px; margin: 2px; display: inline-block; }
.tagit-choice-read-only { background: #f5f5f5; color: #999; }
.tagit-close { margin-left: 5px; color: #999; text-decoration: none; cursor: pointer; }
.tagit-close:hover { color: #f00; }
.tagit-input { border: none; outline: none; padding: 2px; width: 80px; }