gpt4 book ai didi

javascript - 如何添加多个选择选项

转载 作者:行者123 更新时间:2023-12-04 07:26:02 25 4
gpt4 key购买 nike

如何在多个选择选项上添加第 1、2、3 轮,即 第 1 轮 + 第 5 轮 如下图所示,并显示在 id="total"上,即 6 ?我已经尝试过,但没有得到这个想法。
HTML 表单

<div class="form row">

<select class="selectpicker" multiple data-live-search="true" id="no_of_rounds">
<option value="1">Round 1</option>
<option value="2">Round 2</option>
<option value="3">Round 3</option>
<option value="4">Round 4</option>
<option value="5">Round 5</option>
<option value="6">Round 6</option>
<option value="7">Round 7</option>
<option value="8">Round 8</option>
<option value="9">Pay 1st Round - 8th Round</option>
</select>
<input id="total" type="text">
</div>
脚本
<script>
document.querySelector('.form').addEventListener('change', function() {
const nr = +document.getElementById('no_of_rounds').value || 0;
var round_1 = 80;
var round_2 = 90;
var round_3 = 100;
var round_4 = 110;

document.getElementById('total').value = total;
}


}
$('select').selectpicker();
</script>
enter image description here

最佳答案

我认为这就是你要找的:

document.querySelector('.form').addEventListener('change', function() {
let selectedValues = $('#no_of_rounds').val();

let sum = 0;
$("#no_of_rounds option:selected").each(function () {
let $this = $(this);
if ($this.length) {
sum += parseInt($this.val());
}
});
document.getElementById('total').value = sum;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form row">

<select class="selectpicker" multiple data-live-search="true" id="no_of_rounds">
<option value="1">Round 1</option>
<option value="2">Round 2</option>
<option value="3">Round 3</option>
<option value="4">Round 4</option>
<option value="5">Round 5</option>
<option value="6">Round 6</option>
<option value="7">Round 7</option>
<option value="8">Round 8</option>
<option value="9">Pay 1st Round - 8th Round</option>
</select>
<input id="total" type="text">
</div>

关于javascript - 如何添加多个选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68206538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com