我有一段Javascript,它将用户选择的信息转发给外部PHP文件,并返回信息。在下面的代码中,您可以看到它通过POST向该文件发送{'report': report}。
实际上我需要添加另一个要发送的变量。它被称为id,但它在另一个函数中。有没有办法使这个变量全局,然后合并它,所以它发送到我的代码片段?(什么时候全局变量会被清除?)我也可以通过'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'发送到另一个页面以获取信息的代码片段。但是,我需要保留这个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);
}
});
});
听起来像是他们通过链接选择了Company,然后通过另一个链接选择了Report,您需要记住选择了哪个Company。
为了避免全局变量,我可能只需要向所选的Company链接添加一个类,然后通过所选的类获取该元素,并获取其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);
}
});
});
您可以通过将该值赋值为javascript的全局命名空间window的属性来实现这一点。只需将您想要全局化的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);
}
});
});
最后,如果可能的话,我建议不要使用全局变量,或者至少通过在对象中包装通用函数/目的并在名称前加上项目名称或其他东西来减少使用。
变化
data: { 'report': report },
data{ 'report': report, 'id': YOUR ID },
为什么不像这样发送第二个变量呢:
data: {'report': report, 'id': your_id },
编辑:太慢了!