PHP变量到JQuery函数?

标签 php jquery

我在这个函数中需要一个相对路径:

    $(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/

相关文章:

PHP:5.2.x 的 __callStatic 替代方案

javascript - JQuery ajax 忽略 url

javascript - Jquery特选

javascript - 单击第一个图像时允许加载第二个图像,同时保持 WR.CSS 样式。

php - 如何获取 PHP 类中所有函数的数组?

php - 内爆对象数组中给定名称的所有属性 - PHP

php - 在 codeigniter 中使用带有 Like 的 where 子句

php - 在重定向时获取 HTTP Referer

javascript - 我可以使用 jQuery 使 DIV 及其内容可见和不可见吗?

javascript - 如何根据另一个属性更改特定位置的 JSON 对象?