php - 我不能使用 ajax 在我的数据中使用引导实时搜索
问题描述
我正在将 ajax 中的数据导入表中,但是
<select class = "selectpicker" data-live-search = "true">
即使我添加了我的ajax代码也不起作用
$.ajax({
url:'/sbadmin/pages/server/get_brand.php',
type:'GET',
success:function(result){
$('#data').html(result);
}
});
到餐桌
<select class="selectpicker form-control form-control-xs" onChange="window.location.href=this.value" id="data" data-live-search="true">
</select>
get_brand.php
$stmt = $db->prepare('SELECT * FROM brands WHERE brand_id>1 ORDER BY brand_name asc');
$stmt->execute();
$brands = $stmt->fetchAll();
?> <option><-- Choose Brand --></option> <?php
foreach ($brands as $brand)
{
?>
<option data-tokens="<?php echo $user['brand_name'] ?>" value="<?php $_SERVER['DOCUMENT_ROOT'] ?>/sbadmin/pages/order/pharmcy/add-pharmacy-order.php?brand_id=<?php echo $brand['brand_id'] ?>"><a href=""><?php echo $brand['brand_name'] ?></a></option>
<?php
}
解决方案
由于您没有在 AJAX 中传递任何值,请尝试如下。我没有测试,但它会工作
<?php
function dropValues() {
#put connection establishment for DB or declare it globally
$stmt = $db->prepare('SELECT * FROM brands WHERE brand_id>1 ORDER BY brand_name asc');
$stmt->execute();
$brands = $stmt->fetchAll();
$txt = "";
foreach ($brands as $brand)
{
#edit with your option values. I'm just giving the example.
$txt .= '<option data-tokens="'.$user['brand_name'].'" value="'.$_SERVER['DOCUMENT_ROOT'].'">$some value</option> ';
}
return $txt;
}
?>
<select class="selectpicker form-control form-control-xs" onChange="someFunction(this)" id="data" data-live-search="true">
<?php echo dropValues();?>
</select>
<script>
function someFunction(this) {
var val = this.val();
//ajax function here to get the data
//send the val to php page and get result and append it to drop down
}
</script>
您的 ajax php 页面完全没有语法。请访问此处了解有关 ajax 的更多信息https://api.jquery.com/jquery.ajax/
推荐阅读
- google-sheets - 将 IMPORTXML 函数输出格式化为时间“h:mm”
- raspberry-pi - yocto raspberrypi 显示默认启动画面而不是 raspberrypi 启动画面
- r - 如何在因子加载大于0.5的条件下向水平条添加三种颜色
- javascript - 允许在引导标签输入中自由输入(对象作为标签)
- ruby-on-rails - 验证时表单内的简单表单错误通知
- ruby-on-rails - 种子错误 ActiveModel::UnknownAttributeError: 列表的未知属性“摘录”
- python - 在 Nanobox 中,pip install 遇到了 NewConnectionError。无法使用 pip 安装软件包。
- python-3.x - 在 Python 中将 Curses 矩形框扩展到终端的边缘
- excel - 将多个excel表格中的表格粘贴到word文件中,并将word命名为表格名称VBA
- angular - 错误类型错误:无法读取未定义的属性“poster_path”