【发布时间】: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