javascript 从变量设置进度条值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19287260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:59:48  来源:igfitidea点击:

set progress bar value from variable

javascriptjqueryhtml

提问by PSL

I have a variable which updates based on percent file downloaded. How can I get this variable to update a progress bar?

我有一个变量,它根据下载的文件百分比进行更新。如何获取此变量以更新进度条?

var percent = (len / res.headers['content-length']) * 100;

I have tried this to no avail:

我试过这个无济于事:

<progress class="progress"></progress>

$('.progress').val = percent;

回答by PSL

You need to set the value using the setter of .val(newValue). valjust gives you the function reference, you are just resetting it to the value of variable percent, not really assigning it as value.

您需要使用 的设置器设置值.val(newValue)val只是为您提供函数引用,您只是将其重置为变量百分比的值,而不是真正将其分配为值。

Change

改变

 $('.progress').val = percent;

to

 $('.progress').val(percent);

You could also do $('.progress')[0].value = percent. probably that is what you had in mind. But valin jquery is used as a function, (more like getter, setter kind of functionality).

你也可以这样做$('.progress')[0].value = percent。可能这就是你的想法。但是val在 jquery 中被用作函数,(更像是 getter、setter 类的功能)。

Also do remember that progress element takes value ranging from 0.0to 1.0or the value of the max attribute (if present).

还要记住,progress 元素的值范围为0.0to1.0或 max 属性的值(如果存在)。

回答by Rohan Kumar

Pass valuein val()like,

通过valueVAL()一样,

$('.progress').val(percent);

HTML

HTML

<progress class="progress" value="10" max="100"></progress>

Fiddle

小提琴

回答by Anup

Your method is wrong in jquery you need to write like this:

$('.progress').val(percent);

回答by Vinay Pratap Singh

you are giving in wrong way

你以错误的方式给予

give like this

像这样给

 $('.progress').val(percent);

see val()

val()