【问题标题】:Sum radio inputs values [duplicate]求和无线电输入值[重复]
【发布时间】:2021-08-15 02:31:45
【问题描述】:

我有这样的无线电输入:

<input type="radio" name="battery" class="radio" value="0" id="63" />
<label for="63">SoH 63-65%</label>
<input type="radio" name="battery" class="radio" value="100" id="66" />
<label for="66">SoH 66-67%</label>
<input type="radio" name="battery" class="radio" value="200" id="68" />
<label for="68">SoH 68-69%</label>

还有这个:

    <input type="radio" name="year" class="radio" value="400" id="2015" />
    <label for="2015">2015 year</label>
    <input type="radio" name="year" class="radio" value="500" id="2016" />
    <label for="2016">2016 year</label>

点击此按钮,它们的值会汇总所有值

<button class="load-more-btn" id="sumUp>
  Sum up
</button>

结果应该用这个脚本输出:

document.getElementById("price").innerHTML = RESULTVALUE;

我该怎么做?

【问题讨论】:

标签: javascript html jquery sum


【解决方案1】:

您可以使用querySelector:checked pseudo-class 来获取选中的单选按钮,然后将它们的值相加:

sumUp.addEventListener('click', function(){
  var sum = +document.querySelector('[name="battery"]:checked').value + +document.querySelector('[name="year"]:checked').value;
  document.getElementById("price").innerHTML = sum;
})
<input type="radio" name="battery" class="radio" value="0" id="63" />
<label for="63">SoH 63-65%</label>
<input type="radio" name="battery" class="radio" value="100" id="66" />
<label for="66">SoH 66-67%</label>
<input type="radio" name="battery" class="radio" value="200" id="68" />
<label for="68">SoH 68-69%</label>

<input type="radio" name="year" class="radio" value="400" id="2015" />
<label for="2015">2015 year</label>
<input type="radio" name="year" class="radio" value="500" id="2016" />
<label for="2016">2016 year</label>
<button class="load-more-btn" id="sumUp">
  Sum up
</button>
<div id="price"></div>

【讨论】:

    猜你喜欢
    • 2015-10-26
    • 2019-11-26
    • 2018-07-08
    • 1970-01-01
    • 2018-10-02
    • 2012-03-24
    • 2017-03-24
    • 1970-01-01
    • 2021-09-23
    相关资源
    最近更新 更多