0

我有一个包含 3 个部分的页面。左侧是静态菜单

例如

<a href="#" class="actionLink" id="newFile"><li>New File</li></a>

这可以开始新的动作,例如

$('.actionLink').click(function() {
    var folderID; // trying set ID here
    var fileName = $(this).attr('id');
    $("#myAction").load('/ajax/actions/' + fileName + '.php?folder_id=' + folderID);
})

所以,这加载/ajax/actions/newFile.php

中间是使用 jquery 加载的页面.load()。在中间的页面内是一系列具有 ID 的文件夹。单击时,这些文件夹会显示其内容,显示在页面右侧。

例如

<span id="12" class="folder active99">Music</span>

$('.folder').click(function() {
    var folderID = $(this).attr('id');
    $("#myAction").load('/ajax/actions/links.php?folder_id=' + folderID);
})

单击时会在右侧显示内容。注意变量文件夹ID。这一切正常。

What I would like to happen is when a folder is selected in the middle, it changes the folderIDvariable on the left hand menu so when a new action is chosen it corresponds to the folder its supposed to.

我已经尝试在任何地方设置变量,即在所有部分中var folderID;,但无论我尝试什么都不会携带变量。

这是可能的还是有更好的方法来做到这一点?我做错了吗?

总结一下:当我单击中间的文件夹时,我需要将变量添加到左侧菜单中。

更新

这是我目前使用的代码:

$(document).ready(function(){
            var folderID = '';
            $('.actionLink').click(function() {
                var fileName = $(this).attr('id');
                $("#myAction").load('/ajax/actions/' + fileName + '.php?folder_id=' + folderID);
            });

            $('.folder').click(function() {
                $('.folder').removeClass('active99'); // remove from all other <SPAN>s
                $(this).addClass('active99'); // add onto current
                var folderID = $(this).attr('id');
                $("#myAction").load('/ajax/actions/links.php?folder_id=' + folderID);
            });
        });

我现在稍微改变了一些东西,所以实际上包含了中间部分,而不是使用.load()但仍然无法正常工作

4

1 回答 1

1

您正在处理变量范围问题,您必须在外部(在更大范围内)声明 folderID 变量,以便它可用于两种操作:

$(document).ready(function(){
    var folderID = '';
    $('.actionLink').click(function() {
        var fileName = $(this).attr('id');
        $("#myAction").load('/ajax/actions/' + fileName + '.php?folder_id=' + folderID);
    });

    $('.folder').click(function() {
        folderID = $(this).attr('id');
        $("#myAction").load('/ajax/actions/links.php?folder_id=' + folderID);
    });
});
于 2013-10-06T21:19:29.677 回答