2012-03-15 97 views
0

我想在使用javascript的HTML中创建一个动态栏。 我已经创建了按钮,但似乎无法将值传递给进度条。 有人可以帮我吗?谢谢!动态进度条Javascript和HTML

<button onclick="increase()">Add</button> 
<button onclick="decrease()">Minus</button> 
<input type="text" id="tb"> 
<script type="text/javascript"> 
var value = 0 document.getElementById("tb").value = value; 
function increase(){ 
    this.value = value + 1; document.getElementById("tb").value=value;  
} 
function decrease(){ 
    this.value = value - 1; document.getElementById("tb").value=value; 
} 
document.write("<div class='meter'><span style='width: 30%'></span> </div>"); 
document.write("<input type='text' id=\"tb\">"+value +" </input>"); 
</script> 
+0

<按钮的onclick = “增加()”>添加 <按钮的onclick = “降低()” >减号 \t \t <脚本类型= “文本/ JavaScript的”> \t \t \t VAR值= 0 \t \t \t的document.getElementById( “TB”)值=值。 \t \t \t \t \t \t函数增大(){ \t \t \t THIS.VALUE =值+ 1; \t \t \t document.getElementById(“tb”)。value = value; \t \t \t \t} \t \t \t \t \t \t功能减退(){ \t \t \t THIS.VALUE =值 - 1; \t \t \t document.getElementById(“tb”)。value = value; \t \t \t} \t \t \t \t \t \t文件撰写( “

\t
”); \t \t \t \t document.write(“”+ value +“”); \t \t – 2012-03-15 20:19:00

+0

http://jsfiddle.net/(有代码) – Alp 2012-03-15 20:19:09

+0

你可以给我们提供一些代码吗? – 2012-03-15 20:19:27

回答

1

它会更容易做到这一点jQuery的,但在这里它去与POJS:

JS:

var value = 0, 
tb = document.getElementById("tb"), 
progress = document.getElementById("progress"); //store these, it's better 
function increase(){ 
    value++;// same as value += 1, but better 
    if(value>=100) value = 100;//keep it under 100% 
    tb.value = value;// set the value of the text field  
    progress.style.width = value + "%";// set the width of the progress bar 
} 
function decrease(){ 
    value--; 
    if(value<=0) value = 0;//keep it over 0% 
    tb.value = value; 
    progress.style.width = value + "%"; 
} 

document.write是janky,所以我抛弃这&把酒吧在标记中。

HTML:

<button onclick="increase()">Add</button> 
<button onclick="decrease()">Minus</button> 
<input type="text" id="tb"> 
<div id='meter'><div id='progress'></div></div> 

CSS:

​#meter {border:1px solid #000;width:100px} 
#progress {background:#333;height:10px;width:0%}​ 

拨弄:http://jsfiddle.net/sw95b/

+0

我尝试了小提琴,但它似乎没有工作,也许你可以发送给我 – 2012-03-16 01:20:35