【问题标题】:Sending a global variable发送一个全局变量
【发布时间】:2011-10-26 13:01:15
【问题描述】:

我有一段 Javascript 将用户选择的信息转发到外部 PHP 文件,并返回信息。在下面的代码中,您可以看到它通过 POST 将 {'report' : report} 发送到该文件。效果很好。

基本上我需要添加另一个要发送的变量。它被称为“id”,但它在另一个函数中。有没有办法使该变量成为全局变量,然后将其合并到我的代码 sn-p 中? (以及何时清除全局变量?)我也可以通过 'url' 属性发送它,并在我的 PHP 中使用 GET ......只是不知道如何实现。

$('#adminSelectReport a').live("click", function () {
    //Get id from clicked link:
    var report = $(this).attr('id');

    $.ajax({
        type: 'POST',
        url: 'getReportInfo.php',
        data: {
            'report': report
        },
        success: function (msg) {
            //everything echoed in your PHP-File will be in the 'msg' variable:
            $('#adminReportInfo').html(msg);
            $('#adminReportInfo').fadeIn(400);
        }
    });
});

UPDATE:这是另一个将“id”发送到另一个页面以获取信息的 sn-p。但是,我需要保留此 ID,并将其用于我的原始代码。

$('#adminSelectCompany a').click(function() {
    //Get id from clicked link:
    var id = $(this).attr('id');

    $.ajax({
        type: 'POST',
        url: 'getReports.php',
        data: {'id': id},
        success: function(msg){
            //everything echoed in your PHP-File will be in the 'msg' variable:
            $('#adminSelectReport').html(msg);
            $('#adminSelectReport').fadeIn(400);
           $('#adminReportInfo').fadeOut(300);

        }
});
    });

【问题讨论】:

  • 函数什么时候被调用?如果它是从您的 .live() 处理程序中调用的,那么您可以将其 return 设置为您想要的值吗?似乎其他功能是关键代码。也许您应该将其包含在问题中。
  • 顺便说一下,用this.id代替$(this).attr('id')
  • @patrick:另一个函数(具有 ID)直接在我包含的代码上方调用。从字面上看,它上面有几行。我将编辑我的帖子以包含它

标签: php javascript mysql html css


【解决方案1】:

您可以通过将该值指定为 JavaScript 的“全局”命名空间窗口的属性来实现这一点。只需将您想要全局化的 id 分配给window.my_id,然后在点击回调中引用它。

注意:如果您在另一个函数中设置全局变量,请记住检查它是否存在于将使用该变量的函数中,即:var my_id = null; if (window.my_id != undefined) { my_id = window.my_id; }

这是一个实现:

$('#adminSelectReport a').live("click", function () {
    //Get id from clicked link:
    var report = $(this).attr('id');
    var company = window.report_company_id != undefined ? window.report_company_id : null;

    $.ajax({
        type: 'POST',
        url: 'getReportInfo.php',
        data: {
            'report': report,
            'company': company
        },
        success: function (msg) {
            //everything echoed in your PHP-File will be in the 'msg' variable:
            $('#adminReportInfo').html(msg);
            $('#adminReportInfo').fadeIn(400);
        }
    });
});

.

$('#adminSelectCompany a').click(function() {
    //Get id from clicked link:
    var id = $(this).attr('id');

    window.report_company_id = id;

    $.ajax({
        type: 'POST',
        url: 'getReports.php',
        data: {'id': id},
        success: function(msg){
            //everything echoed in your PHP-File will be in the 'msg' variable:
            $('#adminSelectReport').html(msg);
            $('#adminSelectReport').fadeIn(400);
           $('#adminReportInfo').fadeOut(300);

        }
});
    });

最后,如果可能的话,我建议不要使用全局变量,或者至少通过将常用函数/用途包装在对象中并在名称前加上项目名称或其他名称来尽量减少使用。

【讨论】:

  • 我将如何实现它?对不起,这是新的:/
【解决方案2】:

所以听起来他们通过一个链接选择了一个公司,然后他们通过另一个链接选择了一个报告,你需要记住选择了哪个公司。

为了避免全局变量,我可能只是将一个类添加到选定的公司链接,然后通过选定的类获取该元素,并获取其 ID。如果需要,您也可以使用该类进行样式设置。

var companies = $('#adminSelectCompany a');

companies.click(function () {

      // remove class from previously selected, and add to new one
    companies.filter('.selected').removeClass('selected');
    $(this).addClass('selected');

    $.ajax({
        type: 'POST',
        url: 'getReports.php',
        data: {
            'id': this.id
        },
        success: function (msg) {
            //everything echoed in your PHP-File will be in the 'msg' variable:
            $('#adminSelectReport').html(msg)
                                   .fadeIn(400);
            $('#adminReportInfo').fadeOut(300);

        }
    });
});
$('#adminSelectReport a').live("click", function () {

    $.ajax({
        type: 'POST',
        url: 'getReportInfo.php',
        data: {
            'report': this.id,
            'company': $('.selected')[0].id
        },
        success: function (msg) {
            //everything echoed in your PHP-File will be in the 'msg' variable:
            $('#adminReportInfo').html(msg);
            $('#adminReportInfo').fadeIn(400);
        }
    });
});

【讨论】:

  • 它似乎有效,但不幸的是打破了第二个菜单(adminSelectReport)的突出显示。当您单击该菜单时,它不再突出显示?
  • @Zakman411:我不知道为什么会这样。你已经在使用.selected这个类了吗?尝试不同的课程。
  • 知道了!我有一个语法错误。谢谢!像魅力一样工作
【解决方案3】:

你为什么不这样发送第二个变量:

 data: {'report': report, 'id': your_id },

编辑:arf 太慢了!

【讨论】:

  • 再看问题。 “它被称为'id',但它在另一个函数中。有没有办法使该变量成为全局变量......”
【解决方案4】:

改变

data: { 'report': report },

data{ 'report': report, 'id': YOUR ID },

【讨论】:

  • 再看问题。 “它被称为'id',但它在另一个函数中。有没有办法使该变量成为全局变量......”
  • 如果链接上有一个 id,就像评论建议的那样,只需拿起它。在 js 中,您可以使用其他函数中的 window.my_global_id = "whatever"。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
  • 1970-01-01
  • 2018-01-20
相关资源
最近更新 更多