我在这个函数中需要一个相对路径:
$(function() {
$("#searchbox").autocomplete({
minLength : 2,
source : function (request, response){
$.ajax({
url : "http://linux/project/index.php/main/search/",
dataType : "json",
data : { key : request.term},
type : "POST",
success : function(data){
response($.map(data, function(item) {
return {
label: item.original_name,
value: item.original_name,
id : item.project_id+"/"+item.folder_id+"/"+item.id
}
}))
}
})
},
select : function(event, ui) {
document.location.href = "http://linux/project/index.php/projects/loaddocument/"+ui.item.id;
}
});
});
如何使用 PHP 变量路径替换 http://linux/project在上面的函数中?
最好的问候......
最佳答案
url : "http://<?php echo $path; ?>/index.php/main/search/"
或者,如果脚本位于单独的 .js 文件中,则如下所示:
// in the main page
<script type="text/javascript" charset="utf-8">
var config = { basePath : '<?php echo $path; ?>' };
</script>
// in the .js file
url : "http://" + config.basePath + "/index.php/main/search/"
关于PHP变量到JQuery函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2903155/