简体   繁体   English

如何在Javascript / Ajax中调用特定文件夹

[英]How to call a Specific Folder in Javascript/Ajax

I've created a combo ajax select menu and am running it on localhost. 我创建了一个组合的ajax选择菜单,并在本地主机上运行它。 I just tried to move the files into a wordpress config setting it up as a template, but I'm having a problem because of the path for the .js file to call other files. 我只是试图将文件移动到wordpress配置中,将其设置为模板,但是由于.js文件调用其他文件的路径而出现问题。

Is there a way I can call a direct path? 有没有办法我可以称之为直接路径? The JS looks like this: JS看起来像这样:

    $(document).ready(function(){
        $("select#direction").attr("disabled","disabled");
        $("select#route").change(function(){
        $("select#direction").attr("disabled","disabled");
        $("select#direction").html("<option>wait...</option>");
        var id = $("select#route option:selected").attr('value');
        $.post("select_direction.php", {id:id}, function(data){
            $("select#direction").removeAttr("disabled");
            $("select#direction").html(data);
        });
    });
});

I'd like to call it from the wordpress theme folder/includes/select_direction.php 我想从wordpress主题文件夹/includes/select_direction.php中调用它

This JS file exists in the includes folder as well. 该JS文件也存在于include文件夹中。

You can use the path from the websites document root directly to the file you want to call. 您可以使用从网站文档根目录直接到您要调用的文件的路径。 May be something like: 可能是这样的:

"/theme_folder/includes/select_direction.php"

Any path that begins with a slash "/" will be a path originating from your document root. 任何以斜杠“ /”开头的路径都是从文档根目录开始的路径。 If the path is relative to your the page your JavaScript code is on, you can use dot-dot ".." to mean up one directory: 如果路径相对于您的JavaScript代码所在的页面,则可以使用点号“ ..”来表示一个目录:

"../../theme-folder/includes/select_direction.php"

If you want to pass this path from your PHP script, the best option would be to put the path in an HTML element attribute: 如果要从PHP脚本传递此路径,最好的选择是将路径放入HTML元素属性中:

<body data-script-path="/theme_folder/includes/select_direction.php">

Then in your JavaScript code you can get the path with: 然后,在您的JavaScript代码中,您可以通过以下方式获取路径:

var path = $("body").data("script-path");

There are many ways to pass data from the server side to the client side, you could parse the JavaScript with PHP -- but that is an ugly solution. 有很多方法可以将数据从服务器端传递到客户端,您可以使用PHP解析JavaScript,但这是一个丑陋的解决方案。 HTML5 data attributes are much cleaner. HTML5数据属性更加简洁。

The JavaScript code does not know it is in the includes folder. JavaScript代码不知道它在include文件夹中。 It runs where ever the page it is running on is located. 它会在运行页面的任何位置运行。

So you would reference files like you would with any relative path from where the page is located. 因此,您将引用文件,就像使用页面所在位置的任何相对路径一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM