简体   繁体   English

在 JavaScript 中获取更新的全局变量 - jQuery

[英]Getting updated global variable in JavaScript - jQuery

I am getting undefined type for:我得到undefined的类型:

$(".ui-slider-handle").attr("title", '"'+current+'"');

As you can see I tried to alert the current in line 29 and alerts the correct updated current value but it is not functioning on .attr("title", '"'+current+'"') .如您所见,我尝试在第 29 行警告current值并警告正确更新的current值,但它在.attr("title", '"'+current+'"')上不起作用。 Why is this happening, and how can I solve this issue?为什么会发生这种情况,我该如何解决这个问题?

 $(function () {
     var current;
     $("#slider-vertical").slider({
         orientation: "vertical",
         range: "min",
         min: 0,
         max: 100,
         value: 60,
         slide: function (event, ui) {
             $("#amount").val(ui.value);
             var offsets = $('.ui-slider-handle').offset();
             var top = offsets.top;
             $(".tooltip").css('top', top - 90 + "px");
             $(".tooltip-inner").text(ui.value);

             function setCurrent() {
                 current = ui.value;
             }
             setCurrent();
         }
     });
     $("#amount").val($("#slider-vertical").slider("value"));


     $(".ui-slider-handle").attr("rel", "tooltip");
     $(".ui-slider-handle").attr("data-toggle", "tooltip");
     $(".ui-slider-handle").attr("data-placement", "left");

     //  alert(current);
     //   $(".ui-slider-handle").attr("title", "50");
     $(".ui-slider-handle").attr("title", '"'+current+'"');

     $("[rel='tooltip']").tooltip();
 });

What do you have the single-double-single quote for?你有什么单双单引号? Try to use:尝试使用:

$(".ui-slider-handle").attr("title", current);

Does it require double quotes to appear?是否需要出现双引号?

I'm gonna leave that cause it was boneheaded and funny.我要离开那个,因为它是愚蠢和有趣的。 Anyway, current is inside the scope of the function and does not have a value outside of it.无论如何, current 在函数的范围内,并且在它之外没有值。 Try this instead: Change this:试试这个:改变这个:

function setCurrent() {
current = ui.value;
}
setCurrent();

to

current = ui.value;

Or you could just return the value like this:或者你可以像这样返回值:

function setCurrent() {
return ui.value;
}

current = setCurrent();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM