我一直在爲此而努力,整天過,從這裏那裏到處都在數據的屑拉動。我創建了一個簡單的項目,以消除所有外部影響,直到我得到它的工作,然後我能夠成功地將其轉移到我的項目中。
這是我的裸骨項目,您應該能夠使用它來證明您可以使用這個概念,並將其作爲與您的項目進行比較的工作示例。
自己創建一個HTML文件,並粘貼英寸
<!DOCTYPE html>
<html>
<head>
<title>TagsInput and TypeAhead TOGETHER!</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/bootstrap-tagsinput.css">
</head>
<body>
<h1>TagsInput and TypeAhead TOGETHER!</h1>
<select id="Country">
<option>UK Cars</option>
<option>German Cars</option>
</select>
<input type="text" id="carsSearch" placeholder="Type Car Names" />
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/bootstrap-tagsinput.min.js"></script>
<script type="text/javascript" src="js/bootstrap3-typeahead.js"></script>
<script type="text/javascript" src="js/tagsinput and typeahead.js"></script>
</body>
</html>
創建一個js文件夾和文件名爲tagsinput和typeahead.js並粘貼此英寸
$(document).ready(
function() {
// Call the data population
bindCarList();
}
);
bindCarList = function() {
// Call TagsInput on the input, and set the typeahead source to our data
$('#carsSearch').tagsinput({
typeahead: {
source: function() {
if ($('#Country').val() == "UK Cars") {
return ["Lotus", "TVR", "Jaguar", "Noble", "Land Rover", "Rover"];
} else {
return ["BMW", "Audi", "Volkswagen", "Mercedes-Benz"];
}
}
}
});
$('#carsSearch').on('itemAdded', function (event) {
// Hide the suggestions menu
$('.typeahead.dropdown-menu').css('display', 'none')
// Clear the typed text after a tag is added
$('.bootstrap-tagsinput > input').val("");
});
}
您需要下載一些文件,但是我儘可能使用CDN來將其保持在最低限度。
這是不完美。我必須使用解決方法清除搜索文本,並在添加標籤後隱藏列表。你不能從輸入中刪除,這不是很好。這很好,也許有人可以改進它。
來源
2015-08-06 15:42:55
Red
可能重複[Typeahead插件和引導標籤輸入插件流星不工作](http://stackoverflow.com/questions/21641417/typeahead-plugin-and-bootstrap-tags-input-plugin-meteor-not-working ) –