如何在 jQuery Slider 函数中创建全局变量

发布于 2024-12-11 18:33:32 字数 443 浏览 0 评论 0原文

我的脚本中有一个滑块:

var uniquePrice;
$( "#slider-secondary" ).slider({
  range: "min",
  value: 0,
  min: 0,
  max: 50,
  slide: function( event, ui ) {
    $( "#amount-secondary" ).val( ui.value );
            uniquePrice = ui.value * 1500;
  }
});
console.log(uniquePrice);

并且我需要在滑块函数之外的代码中进一步使用 uniquePrice var。它不起作用 - console.log(uniquePrice);说“未定义”。

我做错了什么?

PS 当然,我在函数之前定义了全局变量,抱歉忘记在这里包含。

I have a slider inside my script:

var uniquePrice;
$( "#slider-secondary" ).slider({
  range: "min",
  value: 0,
  min: 0,
  max: 50,
  slide: function( event, ui ) {
    $( "#amount-secondary" ).val( ui.value );
            uniquePrice = ui.value * 1500;
  }
});
console.log(uniquePrice);

And I need to use the uniquePrice var further in the code, outside of the slider function. And it doesn't work - console.log(uniquePrice); says "undefined".

What am I doing wrong?

P.S. Of course I defined the global variable before the function, sorry for forgetting to include here.

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

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

发布评论

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

评论(3

我很坚强 2024-12-18 18:33:33

您必须在函数外部定义变量。

var uniquePrice;
$( "#slider-secondary" ).slider({
      range: "min",
      value: 0,
      min: 0,
      max: 50,
      slide: function( event, ui ) {
          $( "#amount-secondary" ).val( ui.value );
          uniquePrice = ui.value * 1500;
          console.log(uniquePrice);
      }
});

如果您想要函数之外的变量,您可以执行以下操作:

var uniquePrice;
$( "#slider-secondary" ).slider({
      range: "min",
      value: 0,
      min: 0,
      max: 50,
      slide: function( event, ui ) {
          $( "#amount-secondary" ).val( ui.value );
          uniquePrice = ui.value * 1500;
          doLog();
      }
});

function doLog(){
    console.log(uniquePrice);
}

编辑:将 console.log 添加到滑块函数

You have to define the variable outside of the function.

var uniquePrice;
$( "#slider-secondary" ).slider({
      range: "min",
      value: 0,
      min: 0,
      max: 50,
      slide: function( event, ui ) {
          $( "#amount-secondary" ).val( ui.value );
          uniquePrice = ui.value * 1500;
          console.log(uniquePrice);
      }
});

if you want the variable outside of the function you can do:

var uniquePrice;
$( "#slider-secondary" ).slider({
      range: "min",
      value: 0,
      min: 0,
      max: 50,
      slide: function( event, ui ) {
          $( "#amount-secondary" ).val( ui.value );
          uniquePrice = ui.value * 1500;
          doLog();
      }
});

function doLog(){
    console.log(uniquePrice);
}

edit: added the console.log to the slider function

眉目亦如画i 2024-12-18 18:33:33

我想我在某处读到,如果您忘记将“var”放在变量名称前面,它将被声明为全局变量。请确认或否认这一点;)

PS 如果完全删除第 1 行会发生什么?你会得到什么样的错误?

I think I read somewhere that if you forget to put the 'var' infront of the name of the variable, it gets declared as a global variable. Please confirm or deny this ;)

P.S. What happens if you remove line #1 altogether? What kind of error do you get?

把回忆走一遍 2024-12-18 18:33:32

console.log() 将在该函数设置该变量之前被调用。 js 将:

  • 声明 var
  • 然后设置滑块
  • 立即调用 console.log()

因此,在滑动发生之前,该变量将是未定义的

The console.log() is going to be called before that variable is set by that function. The js will:

  • declare the var
  • then setup the slider
  • the immediately call console.log()

So until the slide happens that variable will be undefined

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