如何将多个选择字段相加,包括减去负数(零)

时间:2019-04-25 15:16:04

标签: javascript math

我正在尝试使用几个选择字段来修改数字起点。选择字段包含一些负数,因此我想相应地加减。我目前无法使用JQuery执行此操作-因此,我试图弄清楚如何使用纯JavaScript。我已经走了这么远,但这仅适用于单个选择字段。出于同样的原因,我不确定负数是否会减去:

<select class="select1 selectables" id="dropdown-1" name="dropdown1"> 
 <option disabled="disabled" selected="selected" value="">select</option> 
 <option value="10">Choice 1 (plus 10)</option>
 <option value="-5">Choice 2 (minus 5)</option>
 <option value="60">Choice 3 (plus 60)</option> 
</select>
 <br />
<select class="select2 selectables" id="dropdown-2" name="dropdown2">    
 <option disabled="disabled" selected="selected" value="">select</option>
 <option value="5">Choice A (plus 10)</option>
 <option value="15">Choice B (minus 10)</option>
 <option value="15">Choice C (plus 15)</option> 
 </select>
 <br />
 <select class="select3 selectables" id="dropdown-3" name="dropdown3">    
  <option disabled="disabled" selected="selected" value="">select</option>
  <option value="5">Choice ii (- 35)</option>
 <option value="15">Choice ii (plus 15)</option>
 <option value="12">Choice iii (plus 12)</option> 
 </select>

<script>
document.addEventListener("DOMContentLoaded", function(event) {
    var gg1 = new JustGage({
      id: "gg1",
      value: 0
      // removed elements, as this area isn't an issue
    });

document.getElementById('dropdown-1').addEventListener('change', (event) => {
      sum = 0;
      testvar = event.target.value;
      if (testvar > 0) {
        sum = (sum + testvar);
        alert(`up ${testvar}`);
      } else if (testvar < 1) {
        sum = (sum - parseInt(testvar, 10));
        alert(`down ${testvar}`);
      }
      gg1.refresh(sum);
      return false;
    });
</script>

1 个答案:

答案 0 :(得分:0)

您不需要对负数进行特殊处理。只需像对待正数一样添加它们即可。在以下代码段中,我将负选项的值更改为它们的实际负值。 calculateSum()然后从所有框中计算总和。不只是改变了。尽管绝对可以使用增量版本,但没有理由只用三个盒子来完成。而且这种增量变体更可能有错误。

document.addEventListener("DOMContentLoaded", function(event) {
  document.getElementById('dropdown-1').addEventListener('change', calculateSum);
  document.getElementById('dropdown-2').addEventListener('change', calculateSum);
  document.getElementById('dropdown-3').addEventListener('change', calculateSum);
});

function calculateSum() {
  sum = 0;
  //iterate all three boxes
  for (var i = 1; i <= 3; ++i) {
    var box = document.getElementById('dropdown-' + i);
    var value = parseInt(box.value);
    if (!isNaN(value))
      sum += value;
  }
  alert(sum);
  return false;
}
<select class="select1 selectables" id="dropdown-1" name="dropdown1">
  <option disabled="disabled" selected="selected" value="">select</option>
  <option value="10">Choice 1 (plus 10)</option>
  <option value="-5">Choice 2 (minus 5)</option>
  <option value="60">Choice 3 (plus 60)</option>
</select>
<br />
<select class="select2 selectables" id="dropdown-2" name="dropdown2">
  <option disabled="disabled" selected="selected" value="">select</option>
  <option value="10">Choice A (plus 10)</option>
  <option value="-10">Choice B (minus 10)</option>
  <option value="15">Choice C (plus 15)</option>
</select>
<br />
<select class="select3 selectables" id="dropdown-3" name="dropdown3">
  <option disabled="disabled" selected="selected" value="">select</option>
  <option value="-35">Choice ii (- 35)</option>
  <option value="15">Choice ii (plus 15)</option>
  <option value="12">Choice iii (plus 12)</option>
</select>