jQuery 在 .click 之后不传递变量

发布于 2024-10-06 02:10:04 字数 1110 浏览 9 评论 0原文

所以我正在运行一个简单的 jQuery 脚本。它有2个按钮,1个发送数据,1个按钮需要设置第一个发送的变量之一的值。

好的,这里是按钮 1 的脚本(称为预览):

$('#execute').click(function() {
$('#currentImage').remove();

$.ajax({
   type: "POST",
   url: "effect.php",
   data: { action: $("[name='action']:checked").val(), source: sourceImage, destination: destinationImage, variables: $('#variables').val() },
   success: function(msg){
     $('#workspace').html(msg);
     $("#preview").attr("src", destinationImage+"?timestamp=" + new Date().getTime());
   }
 }); });

它的作用是对图像添加效果,并且效果非常好。 javascript 的第一行之一包含“sourceImage”变量。

var sourceImage = '20101201161609.jpg';

该变量已正确传递给上面的脚本。 第二个按钮(称为“保存状态”)可确保我对图像的效果感到满意,保存图像的状态,并继续对该图像进行效果。这意味着需要更改源图像,我使用以下脚本:

$('#save_state').click( function() {
var sourceImage = '2010-12-02-35-20-preview.png';
$('#header').html(sourceImage); });

我留下了行 $('#header').html(sourceImage); 来检查它是否确实被触发,确实如此,所以这条线不是必需的,但我确信该操作已被触发。

但是,我再次单击“预览”将变量 sourceImage 的值更改回其开始时的值,而实际上它需要保留这个新值。

希望有人可以帮助我。

So I have a simple jQuery script running. It has 2 buttons, 1 that sends data, and 1 button needs to set the value of one of the variables send in the first one.

Ok so here is the script for button 1 (called preview):

$('#execute').click(function() {
$('#currentImage').remove();

$.ajax({
   type: "POST",
   url: "effect.php",
   data: { action: $("[name='action']:checked").val(), source: sourceImage, destination: destinationImage, variables: $('#variables').val() },
   success: function(msg){
     $('#workspace').html(msg);
     $("#preview").attr("src", destinationImage+"?timestamp=" + new Date().getTime());
   }
 }); });

What this does is put an effect to an image and it all works wonderfull.
One of the first lines of the javascript contains the "sourceImage" variable.

var sourceImage = '20101201161609.jpg';

That variable is correctly passed to the script above.
A second button (called Save State) makes sure that ones I feel happy with the effect of the image I save the state of the image, and continue with effects over that image. This means the source image needs to be changed, which I use the following script for:

$('#save_state').click( function() {
var sourceImage = '2010-12-02-35-20-preview.png';
$('#header').html(sourceImage); });

I left the line $('#header').html(sourceImage); in to check if it was indeed triggered, and it was, so this line is not neccesary, but I know for sure that the action is triggered.

However, ones I again click on "Preview" in changes the value of variable sourceImage back to the value it started with, while it actually needs to keep this new value.

HOpe there's somebody that can help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

明明#如月 2024-10-13 02:10:04

通过使用 var 关键字,您可以在函数中声明一个局部变量,该变量与同名的全局变量分开。删除 var 关键字以访问全局变量:

$('#save_state').click( function() {
  sourceImage = '2010-12-02-35-20-preview.png';
  $('#header').html(sourceImage);
});

By using the var keyword you declare a local variable in the function, that is separate from the global variable with the same name. Remove the var keyword to access the global variable:

$('#save_state').click( function() {
  sourceImage = '2010-12-02-35-20-preview.png';
  $('#header').html(sourceImage);
});
牵你的手,一向走下去 2024-10-13 02:10:04

第二个函数中的 sourceImage 变量的作用域仅限于函数本地,并且不引用同一变量。我认为您只需删除 var 关键字即可使其按照您想要的方式工作。话虽如此,您应该非常小心地使用全局范围的变量。一般来说,您希望尽可能缩小变量的范围。您可能想使用 data 函数将此值存储在 DOM 中,但如果不进一步了解您要做什么就很难知道。

The sourceImage variable in the second function is scoped locally to the function and doesn't refer to the same variable. I think you can make it work the way you want by simply removing the var keyword. Having said that, you should be very careful using globally scoped variables. Generally you want to scope the variable as narrowly as possible. You might want to use the data function to store this value in the DOM instead, though it's hard to know without more understanding of what you are trying to do.

陌伤浅笑 2024-10-13 02:10:04

在这里...

$('#save_state').click( function() {
var sourceImage = '2010-12-02-35-20-preview.png';
$('#header').html(sourceImage); });

...您绝对什么也没做,因为您正在设置local(如var所示)变量sourceImage,该变量立即超出范围。全局(窗口)变量 sourceImage 保持不变。

Here...

$('#save_state').click( function() {
var sourceImage = '2010-12-02-35-20-preview.png';
$('#header').html(sourceImage); });

...you are doing absolutely nothing, because you are setting the local (as indicated by var) variable sourceImage which goes out of scope immediately. The global (window) variable sourceImage remains untouched.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文