[英]How to get sum of all selected data from bootstrap multiselect and show in anoher input?
I want to sum of all selected values of data-price. 我想总结所有选定的数据价格值。 For ex if i select ac repair and fan repair so get the value 2100. I am using bootstrap multiselect here.
如果我选择交流修复和风扇维修,那么得到2100的价值。我在这里使用bootstrap multiselect。
<div class="form-group">
<label>Select Service</label>
<select id="serviceid" multiple="multiple" required>
<option data-price="1000" value="1">maintenance</option>
<option data-price="2000" value="2">ac repair</option>
<option data-price="100" value="3">fan repair</option>
<option data-price="50" value="4">cooler repair</option>
</select>
</div>
您可以使用以下代码获取属性值,
var price= $('#serviceid option:selected').attr('data-price');
To sum the values of all selected values of multi select drop-down, use below code , 要汇总多选下拉菜单的所有选定值的值,请使用以下代码,
var sum=0;
$('#serviceid option:selected').each(function(){
sum+=parseInt($(this).data('price'))
})
First of all thanks to all of you for help me. 首先感谢各位的帮助。 I waste my whole day but i get success to fetch the data.
我浪费了一整天但是我获得了获取数据的成功。 Please see this code if anyone stuck.
如果有人卡住,请查看此代码。 I appreciate to all of you.
我感谢你们所有人。
<script type="text/javascript">
$(document).ready(function(){
$('#serviceid').change(function () {
var selected = $("#serviceid option:selected");
var sum = 0;
selected.each(function () {
var tsum = $(this).val(); //it gives value
psum = $(this).data('price'); //it gives the data-price value
sum += Number(psum);
});
$('#tsum').val(sum);
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.