Twitter bootstrap Bootstrap-3-Typeahead-afterSelect-Get-ID
我正在使用Bootstrap-3-Typeahead从mysql自动完成。 我需要从所选项目名称中获取项目代码Twitter bootstrap Bootstrap-3-Typeahead-afterSelect-Get-ID,twitter-bootstrap,bootstrap-typeahead,Twitter Bootstrap,Bootstrap Typeahead,我正在使用Bootstrap-3-Typeahead从mysql自动完成。 我需要从所选项目名称中获取项目代码 $('#item_code').val(this.item_code); 不幸的是,它不起作用 我的Json输出: [{“项目代码”:“1”,“项目名称”:“A”},{“项目代码”:“2”,“项目名称”:“B”}] 这是我的密码,请告知 $('input.item_name').typeahead({ minLength: 3,
$('#item_code').val(this.item_code);
不幸的是,它不起作用
我的Json输出:
[{“项目代码”:“1”,“项目名称”:“A”},{“项目代码”:“2”,“项目名称”:“B”}]
这是我的密码,请告知
$('input.item_name').typeahead({
minLength: 3,
source: function (query, process) {
$.ajax({
url: 'function/load-item.php',
type: 'POST',
dataType: 'JSON',
data: 'query=' + query,
success: function(data) {
var newData = [];
$.each(data, function(){
newData.push(this.item_name);
});
return process(newData);
}
});
},
afterSelect: function(){
$('#item_code').val( ... ... ... ... );
}
});
我的php代码
<?php
session_start();
include ('../include/connect.php');
$query = 'SELECT item_code,item_name FROM master_item';
if(isset($_POST['query'])){
$query .= ' WHERE item_name LIKE "%'.$_POST['query'].'%"';
}
$return = array();
if($result = $conn->query($query)){
// fetch array
while ($row=mysqli_fetch_assoc($result))
{
$return[] = $row;
}
// free result set
$result->close();
// close connection
$conn->close();
$json = json_encode($return);
print_r($json);
}
尝试使用typeahead:selected
而不是afterSelect
它返回整个对象,您可以在其中获取所需的值,即。item\u code
。我已经创建了这个,您可以在那里看到工作示例
$('.typeahead').on('typeahead:selected', function (e, datum) {
console.log(datum);
$('#item_code').val(datum.item_code);
});
这是为了那些在不久的将来可能有同样问题的人的利益。
下面是使用JQuery和获得相同功能的另一种简单方法:
例子
var url=“codePath/searchCode.php”;
$('.typeahead')。typeahead({
来源:功能(查询、流程){
返回$.get(url,{query:query},函数(数据){
console.log(数据)
返回过程(数据);
});
},
//在控件上选择值后触发此操作
后选择:功能(数据){
//将id打印到开发人员工具的控制台
console.log(data.id);
}
});代码>在旧线程上回复时,您的代码似乎很好,只是错过了afterSelect回调的参数
$('input.item_name').typeahead({
minLength: 3,
source: function (query, process) {
$.ajax({
url: 'function/load-item.php',
type: 'POST',
dataType: 'JSON',
data: 'query=' + query,
success: function(data) {
var newData = [];
$.each(data, function(){
newData.push(this.item_name);
});
return process(newData);
}
});
},
afterSelect: function(args){
$('#item_code').val(args.item_code );
}
});
谢谢你的回复,伙计。这与你的例子完全吻合。但是我需要使用来自php的远程数据。你能帮助我吗?我试图改编这个例子,但运气不好。请告知,谢谢我已经更新了你提供的小提琴,它在这里工作。嘿,伙计,我又需要你的帮助了。复制您的代码并将url更改为上面的我的url后,typeahead始终显示在第5行,并且过滤器不起作用。请再次帮助,谢谢。你能提供代码给我吗?我指的是不起作用的那个。我将url更改为我的url:function/load-item.php。正如我在上面写的我的问题。在('typeahead:selected')上找到了很多答案,但在此之前没有任何效果。谢谢你,迪恩_Kadir@deen_kadir你是真正的冠军,非常感谢