Focus/unfocus an input on range slider handler's use

社会主义新天地 提交于 2021-01-29 06:52:33

问题


I have a form where the inputs values are controlled by range sliders. There's a calculation every time there's a new entry on each input which operates on focusout.

Though, the calculation doesn't work when only the range sliders are used. Is there a way to focus the inputs while using the range sliders? I can't change the way of calculation, because, it's on all the inputs. So, it has to be this way.

My form is bigger, but, here's an example of what I have:

<label class="va1">Option a price 1:<input id="a1" type="number"/></label>
<input type="range" id="slider-a1" min="0" max="100" step="1" value="0"><br>
<label class="va2">Option a price 2:<input id="a2" type="number"/></label>
<input type="range" id="slider-a2" min="0" max="100" step="1" value="0"><br>
<label>Result:</label><input id="a5" type="text" name="total_amt"/>

And here's the JS:

var opt_1 = document.getElementById("slider-a1");
opt_1.oninput = function() {
  document.getElementById("a1").value = opt_1.value;}
var opt_2 = document.getElementById("slider-a2");
opt_2.oninput = function() {
  document.getElementById("a2").value = opt_2.value;}

calculate = function(){
   var optiona1, optiona2, resultss;
   optiona1 = Number(document.getElementById("a1").value);
   optiona2 = Number(document.getElementById("a2").value);
   resultss = parseInt(optiona1)+parseInt(optiona2);
   document.getElementById('a5').value = resultss;}
   $('input[type=number]').on('focusout', calculate);

来源:https://stackoverflow.com/questions/63902809/focus-unfocus-an-input-on-range-slider-handlers-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!