简体   繁体   English

计算多个输入的总和-jQuery Range Slider

[英]Calculate the sum of multiple inputs - jQuery Range Slider

I am attempting to calculate the sum of multiple jQuery range sliders. 我正在尝试计算多个jQuery范围滑块的总和。 There are many results on stackoverflow when it comes to this, most are not what I'm trying to accomplish, and others show 2 sliders as an example, when I try to add more it breaks (not sure what I'm doing really).. 涉及到stackoverflow的结果很多,多数不是我想要完成的,其他示例以2个滑块为例,当我尝试添加更多滑块时会中断(不确定我实际上在做什么) ..

Here is what I have, 6 sliders, and then I want it to calculate the overall rating after the last one. 这是我拥有的6个滑条,然后我希望它计算最后一个滑条之后的总体评分。 http://fasterforms.com/images/111914.png http://fasterforms.com/images/111914.png

Here is a working fiddle, but as I said, when I try to either add onto the example it breaks, or it is for a "text" input and when I made it a jQuery "range" input it doesn't work. 这是一个有用的小提琴,但是正如我说的,当我尝试将其添加到示例中时,它会中断,或者是用于“文本”输入,而当我将其设为jQuery“范围”输入时,它将不起作用。

http://jsfiddle.net/DULYW/2/ http://jsfiddle.net/DULYW/2/


Here is the closest one I've got to work with 2 range sliders, but when I try to add more based off the code it doesn't work either. 这是我要使用2个范围滑块的最接近的滑块,但是当我尝试根据代码添加更多滑块时,它也不起作用。

 <script type="text/javascript"> 
 function calc(A,B,SUM) { 
 var one = Number(A); 
 if (isNaN(one)) { alert('Invalid entry: '+A); one=0; } 
 var two = Number(document.getElementById(B).value);  
 if (isNaN(two)) { alert('Invalid entry: '+B); two=0; } 
 document.getElementById(SUM).value = one + two; 
 } 
 </script> 

Enter a number: 
     <input name="sum1" id="op1" value="" onChange="calc(this.value,'op2','result')" type="range" min="0" max="5" data-highlight="true" /> 
and another number: 
     <input name="sum2" value="" id="op2" onChange="calc(this.value,'op1','result')" type="range" min="0" max="5" data-highlight="true" /> 
Their sum is: 
     <input name="sum" value="" id="result" readonly style="border:0px;"> 

It always seems anything that works normally for me always refuses to work the same way once I add it to my jQuery mobile pages, any help would be awesome. 似乎任何对我正常工作的东西,一旦将其添加到jQuery移动页面中,总是会拒绝以相同的方式工作,任何帮助都会很棒。

http://jsfiddle.net/DULYW/60/ http://jsfiddle.net/DULYW/60/

Add on change event: 添加更改事件:

JS: JS:

$('.add').change(function(){
  var sum = 0
  $('.add').each(function(){
    sum += isNaN(this.value) || $.trim(this.value) === '' ? 0 : parseFloat(this.value);
  });

  $('#total').html(sum);
})

HTML: HTML:

<input class="add" name="sum1" type="range" min="0" max="5" data-highlight="true" />
<input class="add" name="sum1" type="range" min="0" max="5" data-highlight="true" />
<input class="add" name="sum1" type="range" min="0" max="5" data-highlight="true" />
<input class="add" name="sum1" type="range" min="0" max="5" data-highlight="true" />
<p id="total"></p>

For jQuery mobile you need to make sure that you put your code within an appropriate page event so that the widgets have been created. 对于jQuery mobile,您需要确保将代码放入适当的page事件中,以便创建小部件。 So if your sliders were within a data-role=page div with an id="page1", you could handle the pagecreate event and within it, run the addAll to get the initial total and then add a change handler to each of the sliders that also runs the addAll function: 因此,如果您的滑块位于data-role = page div中且具有id =“ page1”,则可以处理pagecreate事件,并在其中进行运行,运行addAll以获得初始总数,然后向每个滑块添加一个更改处理程序还运行addAll函数:

$(document).on("pagecreate", "#page1", function () {
    $(".add").on("change", function () {
        addAll();
    });

    addAll();
});

function addAll() {
    var sum = 0
    $('.add').each(function (){        
        sum += isNaN(this.value) || $.trim(this.value) === '' ? 0 : parseFloat(this.value);        
    });
    $('#total').html(sum);
}

DEMO 演示

Both worked for me, but I ultimately switched mine to ezankers coding because it allowed me to have different names and they didnt have to be all the same. 两者都对我有用,但是我最终将我的代码改为ezankers编码,因为它允许我使用不同的名称,并且它们不必完全相同。

http://jsfiddle.net/ezanker/DULYW/61/ http://jsfiddle.net/ezanker/DULYW/61/

    $(document).on("pagecreate", "#page1", function () {
    $(".add").on("change", function () {
        addAll();
    });

    addAll();
});

function addAll() {
    var sum = 0
    $('.add').each(function (){        
        sum += isNaN(this.value) || $.trim(this.value) === '' ? 0 : parseFloat(this.value);        
    });
    $('#total').html(sum);
}

Thanks for both of your help, you helped me out a lot. 感谢您的帮助,您对我的帮助很大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM