jquery - 在自动完成 AJAX 请求中传递参数
问题描述
我正在研究要集成的自动完成选项。自动完成工作正常。但是当我添加另一个参数变量时,自动完成功能不起作用,猜测有点语法问题。在下面附加的脚本中,我需要将变量 countrycode 传递给 fetch_customers.php
$(document).ready(function($) {
$("#customers").autocomplete({
var countrycode = '<?php echo $agencyid; ?>';
data: {
countrycode: countrycode
},
source: "fetch_customers.php",
minLength: 2,
select: function(event, ui) {
var url = ui.item.id;
if (url != '#') {
location.href = '/view-customer/' + url;
}
},
// optional (if other layers overlap autocomplete list)
open: function(event, ui) {
$(".ui-autocomplete").css("z-index", 1000);
}
});
});
解决方案
您的语法无效。您需要countrycode
在您提供给的对象之外进行定义autocomplete()
。
话虽如此,这不是您在 jQueryUI 自动完成请求中传递数据的方式。相反,您需要在您调用的 URL 的查询字符串中传递值:
$(document).ready(function($) {
$("#customers").autocomplete({
source: "fetch_customers.php?countrycode=<?php echo $agencyid; ?>",
minLength: 2,
select: function(event, ui) {
var url = ui.item.id;
if (url != '#') {
location.href = '/view-customer/' + url;
}
},
open: function(event, ui) {
$(".ui-autocomplete").css("z-index", 1000);
}
});
});
推荐阅读
- html - 菜单响应问题
- javascript - 我可以在功能组件中使用什么来获得与 componentDidMount 相同的行为?
- java - 如何运行扩展另一个目录中的类的 java .class 文件?
- python - 无法安装底图(python)
- tcl - TCL 脚本错误问题:[dim]GetAttrib:缺少当前元素
- sql - 如何获取当前日期到 where 子句?
- python - 从 kivymd 工具栏更改屏幕
- javascript - 使用 React Typescript 在 react-leaflet-markerclusterer 中显示所有集群标记
- r - 使用 sidebarPanel 添加和删除选项卡
- php - 用于查找多级数组的 Laravel 雄辩关系