我试图添加6个不同范围滑块的值并显示结果。 我得到了一个我预期的数字,但我只想弄清楚如何对每个滑块的属性求和。因此,当按下按钮时,我会得到所有的全部内容。
我创建了一个jsfiddle,但它不起作用。见这里:http://jsfiddle.net/Ae5Y4/3/
有什么建议吗?
HTML:
<div class="row">
<div class="large-6 columns">
<div class="range-slider" data-slider data-options="start: 1; end: 500; display_selector: #SliderOutput1;"> <span class="range-slider-handle"></span> <span class="range-slider-active-segment"></span> <input type="hidden"></div>
<div id="SliderOutput1"></div>
<div class="range-slider" data-slider data-options="start: 1; end: 250; display_selector: #SliderOutput2;"> <span class="range-slider-handle"></span> <span class="range-slider-active-segment"></span> <input type="hidden"> </div>
<div id="SliderOutput2"></div>
<div class="range-slider" data-slider data-options="start: 1; end: 200; display_selector: #SliderOutput3;"> <span class="range-slider-handle"></span> <span class="range-slider-active-segment"></span> <input type="hidden"> </div>
<div id="SliderOutput3"></div>
</div>
<div class="large-6 columns">
<div class="range-slider" data-slider data-options="start: 1; end: 500; display_selector: #SliderOutput4;"> <span class="range-slider-handle"></span> <span class="range-slider-active-segment"></span> <input type="hidden"> </div>
<div id="SliderOutput4"></div>
<div class="range-slider" data-slider data-options="start: 1; end: 500; display_selector: #SliderOutput5;"> <span class="range-slider-handle"></span> <span class="range-slider-active-segment"></span> <input type="hidden"> </div>
<div id="SliderOutput5"></div>
<div class="range-slider" data-slider data-options="start: 1; end: 200; display_selector: #SliderOutput6;"> <span class="range-slider-handle"></span> <span class="range-slider-active-segment"></span> <input type="hidden"></div>
<div id="SliderOutput6"></div>
<br/><br/>
<input type="button" value="Sum" onclick="doMath();" />
<br/><br/>
<div id="myResults"></div>
JAVASCRIPT:
$(document).foundation();
function doMath()
{
var my_input1 = $('.range-slider').attr('data-slider');
var my_input2 = $('.range-slider').attr('data-slider');
var my_input3 = $('.range-slider').attr('data-slider');
var my_input4 = $('.range-slider').attr('data-slider');
var my_input5 = $('.range-slider').attr('data-slider');
var my_input6 = $('.range-slider').attr('data-slider');
var sum = 0;
// Add them together and display
var sum = parseInt(my_input1) + parseInt(my_input2) + parseInt(my_input3) + parseInt(my_input4) + parseInt(my_input5) + parseInt(my_input6);
document.getElementById('myResults').innerHTML = sum;
}
答案 0 :(得分:0)
onClick =不是最好的 - 它可以神秘地搞砸(并且非神秘但我不喜欢它)。而是使用JQuery $(...).click(function);
类型的点击监控。
相关守则:
HTML:
...snip...
<input type="button" value="sum" id="sum" />
...snip...
和Javascript(只需在你的javascript末尾添加它 - 在运行之前确保它是onReady或类似的):
$("#sum").click(doMath);
与那个问题无关,我注意到那里有一些片状代码;我建议使用id而不是类或循环遍历类来获取每个成员而不是仅仅调用它六次来获取数字。事实上,我建议你用以下内容替换整个javascript:
$(document).foundation();
function doMath()
{
var sum = 0;
$('.range-slider').each(function() {
sum += parseInt($(this).attr('data-slider'));
});
document.getElementById('myResults').innerHTML = sum;
}
$("#sum").click(doMath);
您实际上会获得正确的总和,而不是6 *(第一个滑块的值)。 Fiddle for this version
答案 1 :(得分:0)
我是这个框架的新手,所以事情有点令人困惑。
我试图创建一个滑块来显示总和,我不认为我已正确完成此操作,因为滑块看起来很破碎。我已经修改了你的javascript,希望将总和分配给一个滑块。 http://jsfiddle.net/Ae5Y4/7/
HTML:
<div class="range-slider-total" data-slider data-options="start: 1; end: 2250; display_selector: #SliderOutput7;"> <span class="range-slider-handle"></span> <span class="range-slider-active-segment"></span> <input type="hidden"></div>
<div id="SliderOutput7"></div>
Javacript:
$(document).foundation();
function doMath()
{
var sum = 0;
$('.range-slider').each(function() {
sum += parseInt($(this).attr('data-slider'));
});
document.getElementById('myResults').innerHTML = sum;
var new_value = sum;
$('.range-slider-total').foundation('slider', 'set_value', new_value);
}
$("#sum").click(doMath);