2014-01-19 65 views
0

我有6個滑塊,每個滑塊的相應值都是0-100。我希望能夠在每個滑塊的右側顯示每個滑塊百分比(相對於總滑塊總數),但我不確定如何做到這一點。顯示HTML5滑塊值旁邊的百分比

我能搶滑塊值上面顯示,以及滑塊值和值,如JavaScript變量的總和,但我不能......

1)更新的百分比(我想我可能需要編寫一個單獨的函數來完成此操作,然後在顯示滑塊值後立即調用它)或

2)顯示百分比值一個有效的整數)在滑塊值的後面。

任何人都可以告訴我如何做到這一點?從技術角度來看,這不應該很困難,但我在這方面經驗不足。我也沒有使用jquery(正如你所知道的)......謝謝!

代碼: http://jsbin.com/owIjEXA/7/edit

<!-- Put divs here... trying to set up the display of percentages --> 
<div id="val_weight1"></div> 
<div id="val_weight2"></div> 
<div id="val_weight3"></div> 
<div id="val_weight4"></div> 
<div id="val_weight5"></div> 
<div id="val_weight6"></div> 

<!-- form start --> 
<form name = "form_weight1" id = "form_weight1" > 
    <text><b> First Weight </b></text> 
    <input id="weight1" input type="range" name="weight1" min="0" max="100" value="40" step="1" onchange="showValue(this); " /> 
    <span id="range">40</span> 
    <text>/ PERCENTAGE%</text> 

    <script type="text/javascript"> 
<!-- Original JS bin that modified code: http://jsbin.com/ISekuSiN/5/edit --> 
    function get_nextsibling(n) { 
     x=n.nextSibling; 
     while (x.nodeType!=1) { 
     x=x.nextSibling; } 
     return x; } 

    function showValue(self) { 
     get_nextsibling(self).innerHTML=self.value; } 
    </script> 
</form> 

<form name = "form_weight2" id = "form_weight2" > 
    <text><b> Second Weight </b></text> 
    <input id="weight2" input type="range" name="weight2" min="0" max="100" value="10" step="1" onchange="showValue(this); " /> 
    <span id="range">10</span> 
    <text>/ PERCENTAGE%</text> 
</form> 

<form name = "form_weight3" id = "form_weight3" > 
    <text><b> Third Weight</b></text> 
    <input id="weight3" input type="range" name="weight3" min="0" max="100" value="20" step="1" onchange="showValue(this); " /> 
    <span id="range">20</span> 
    <text>/ PERCENTAGE%</text> 
</form> 

<form name = "form_weight4" id = "form_weight4" > 
    <text><b> Fourth Weight </b></text> 
    <input id="weight4" input type="range" name="weight4" min="0" max="100" value="5" step="1" onchange="showValue(this); " /> 
    <span id="range">5</span> 
    <text>/ PERCENTAGE%</text> 
</form> 

<form name = "form_weight5" id = "form_weight5" > 
    <text><b> Fifth Weight </b></text> 
    <input id="weight5" input type="range" name="weight5" min="0" max="100" value="5" step="1" onchange="showValue(this); " /> 
    <span id="range">5</span> 
    <text>/ PERCENTAGE%</text> 
</form> 

<form name = "form_weight6" id = "form_weight6" > 
    <text><b> Sixth Weight </b></text> 
    <input id="weight6" input type="range" name="weight6" min="0" max="100" value="20" step="1" onchange="showValue(this); " /> 
    <span id="range">20</span> 
    <text>/ PERCENTAGE%</text> 
</form> 

<script type="text/javascript"> 

    var weightfactor1 = parseInt(document.getElementById("weight1").value) ; 
    var weightfactor2 = parseInt(document.getElementById("weight2").value) ; 
    var weightfactor3 = parseInt(document.getElementById("weight3").value) ; 
    var weightfactor4 = parseInt(document.getElementById("weight4").value) ; 
    var weightfactor5 = parseInt(document.getElementById("weight5").value) ; 
    var weightfactor6 = parseInt(document.getElementById("weight6").value) ; 

    var weight_factor_sum = weightfactor1 + weightfactor2 + weightfactor3 + weightfactor4 + weightfactor5 + weightfactor6 ; 

    document.getElementById('val_weight1').innerHTML = (weightfactor1/weight_factor_sum)*100; 

    document.getElementById('val_weight2').innerHTML = (weightfactor2/weight_factor_sum)*100; 

    document.getElementById('val_weight3').innerHTML = (weightfactor3/weight_factor_sum)*100; 

    document.getElementById('val_weight4').innerHTML = (weightfactor4/weight_factor_sum)*100; 

    document.getElementById('val_weight5').innerHTML = (weightfactor5/weight_factor_sum)*100; 

    document.getElementById('val_weight6').innerHTML = (weightfactor6/weight_factor_sum)*100; 

</script> 

回答

0

試試這個爲出發點:

document.querySelector("#range").addEventListener("change", function(e){ 
    document.querySelector(".range").textContent=e.currentTarget.value; 
}) 

這裏是Fiddle一起玩。

當幾個滑塊打交道,它沒有任何意義添加change事件每個滑塊,而不是使用event-delegation(你附加change章24 -listener包含所有滑塊的父元素和自己發出事件) 。

這是一個Fiddle處理更多滑塊和總和字段。