Javascript 在引导类型中隐藏值
我正在使用Bootstrap Typeahead和PHP来显示使用source.PHP的数据库中的列表:Javascript 在引导类型中隐藏值,javascript,php,twitter-bootstrap,Javascript,Php,Twitter Bootstrap,我正在使用Bootstrap Typeahead和PHP来显示使用source.PHP的数据库中的列表: <?php if (isset($_POST['query'])) { require( "config.php" ); $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $query = $_POST['query']; $sql = "SELECT * FROM articles WHERE title LIKE '%{
<?php
if (isset($_POST['query'])) {
require( "config.php" );
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$query = $_POST['query'];
$sql = "SELECT * FROM articles WHERE title LIKE '%{$query}%'";
$array = array();
foreach ($conn->query($sql) as $row) {
$array[] = $row['title'] . ',' . $row['id'];
}
// Return the json array
echo json_encode($array);
}
?>
在document.location开头的行中,我将两个值之间的逗号替换为正斜杠,它可以工作,例如/england/123/。但是,它的屏幕显示的是英格兰,123而不仅仅是英格兰
任何帮助都将不胜感激。OK成功获得以下结果: PHP: JS:
不是你问题的确切答案,但这可能会对你有所帮助。。
$('#typeahead').typeahead({
source: function (query, process) {
$.ajax({
url: "source.php",
type: "POST",
data: 'query=' + query,
dataType: 'JSON',
async: true,
success: function(data){
process(data);
}
})
},
sorter: function (items) {
items.unshift(this.query); // Includes a new row with exact search query
return items.sort();
},
updater: function (item) {
document.location = "/companies/" + item.replace(/ /g, '-').replace(/\,/g,'/').toLowerCase() + "/";
return item;
}
});
if (isset($_POST['query'])) {
require( "config.php" );
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$query = $_POST['query'];
$sql = "SELECT * FROM articles WHERE title LIKE '%{$query}%'";
$array = array();
foreach ($conn->query($sql) as $row) {
$array[] = array('label' => $row['title'], 'id' =>$row['id']);
}
// Return the json array
echo json_encode($array);
}
$('#typeahead').typeahead({
source: function (query, process) {
$.ajax({
url: "http://www.stubowker.com/amex/cms/source.php",
type: "POST",
data: 'query=' + query,
dataType: 'JSON',
async: true,
success: function(data){
objects = [];
map = {};
$.each(data, function(i, object) {
map[object.label] = object;
objects.push(object.label);
});
process(objects);
}
})
},
sorter: function (items) {
items.unshift(this.query); // Includes a new row with exact search query
return items.sort();
},
updater: function (item) {
document.location = "/companies/" + map[item].label.replace(/ /g, '-').toLowerCase() + "/" + map[item].id +
"/";
return item;
}
});