【发布时间】:2017-01-10 21:27:54
【问题描述】:
我有一个带有 Bootstrap 框架的网站。我有两个非常讨厌的输入字段,因为我无法让它们正常工作...
一个是这个 Bootstrap Typeahead 输入字段:
<input type="text" id="typeahead" name='name' placeholder='The name' class="form-control" data-provide="typeahead" autocomplete="off" />
另一个是这个下拉列表:
<div class="btn-group">
<button class="btn btn-default dropdown-toggle" id="dates-dropdown-button" type="button" data-toggle="dropdown" href="#">
<?=$reminder_table_th_date?>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1" id="name-dates-dropdown">
</ul>
</div>
(<li> 列表通过 Ajax 调用填充)
两者我都想做:如果我在 ajax 下拉字段中使用预先输入或使用下拉列表选择一个项目,则所选项目应该是该字段显示的值。我尝试了很多很多方法,例如:
How to Display Selected Item in Bootstrap Button Dropdown Title
Bootstrap typeahead - How to set the default value manually
但它们都不起作用,我不知道为什么。我根本不是 JS/jQuery 大师,但这很可疑。我应该将这个函数的 jQuery 代码放在哪里以及如何放置?
PS:Firefox Firebug 没有显示任何 JS 错误,它们只是什么都不做,没有设置值或者没有调用它们的函数。
这是 typeahead Ajax 调用的 jQuery 代码,下拉列表(这很好用),还有一行应该设置 typeahead 值(在更新程序中),但遗憾的是,这不起作用:
<script type="text/javascript">
$(document).ready(function() {
$('#typeahead').typeahead({
source: function (query, process) {
$.ajax({
url: '/functions/name-autocomplete.php',
type: 'POST',
dataType: 'JSON',
data: 'query=' + query,
success: function(data) {
process(data);
}
});
},
displayText: function(item) {
return item
},
updater: function (item) {
$('#typeahead').typeahead('val', item);
$.ajax({
url: '/functions/name-dates-autocomplete.php',
type: 'POST',
dataType: 'JSON',
data: 'query=' + item,
success: function(data) {
$('#name-dates-dropdown').html(data);
}
});
}
});
});
</script>
【问题讨论】:
-
所选项目应该是字段显示的值你能详细说明一下吗?
-
当然。输入文本字段应将此设置为 VALUE,将下拉列表设置为选定项,因此将值替换为现在的一个 PHP 变量:=$reminder_table_th_date?>
-
为了将
- 元素设置为选中状态,我们需要识别该元素。返回的
<li>的样本会很有用 - 元素设置为选中状态,我们需要识别该元素。返回的
-
但这与预输入文本或
<?=$reminder_table_th_date?>有什么关系?我怎么能说“2 月 27 日”是我想要标记为选中的?
标签: javascript jquery html twitter-bootstrap