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
set progress bar value from variable
提问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)
. val
just 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 val
in 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.0
to 1.0
or the value of the max attribute (if present).
还要记住,progress 元素的值范围为0.0
to1.0
或 max 属性的值(如果存在)。
回答by Rohan Kumar
回答by Anup
Your method is wrong in jquery you need to write like this:
$('.progress').val(percent);