2012-03-15 99 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