使用AJAX,我可以从按钮单击中提取数据值,但是可以确保将此值传递给同一页面上另一个按钮中的参数吗?
的test.html:
<a href="#" onClick="image_check()">Activate</a>
<a href="#" onClick="fader(image_number)">Fader</a>
test.js:
function image_check() {
var request = $.ajax({
url: "current_image.php",
type: "GET",
dataType: "html",
success: function(data) {
alert(data);
}
});
}
php文件连接到数据库并提取最新的图像编号 - 它工作正常,警报框显示正确的值。那么,确保使用此“数据”值更新“image_number”参数的下一步是什么?
干杯。
答案 0 :(得分:1)
制作一个像
这样的全局变量windows.image_number = 0;
用于AJAX功能。
function image_check() {
var request = $.ajax({
url: "current_image.php",
type: "GET",
dataType: "html",
success: function(data) {
//update the global variable
windows.image_number = data;
}
});
}
答案 1 :(得分:1)
假设您传递给函数的image_number
变量是全局定义的变量,您只需在成功回调中设置变量:
function image_check() {
var request = $.ajax({
url: "current_image.php",
type: "GET",
dataType: "html",
success: function(data) {
alert(data);
// Assuming data holds the image number you want to use for next click
image_number = data;
}
});
}