简体   繁体   English

如何从bootstrap multiselect中获取所有选定数据的总和并显示在其他输入中?

[英]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.

相关问题 如何从多选输入中获取所有选定的值? - How to get all the selected values from a multiselect input? 使用PHP从引导多重选择中获取所有选定值? - Get all selected values from bootstrap multiselect using PHP? 如何从yii2中的多选复选框输入中选择用户? - how to get user selected from multiselect checkbox input in yii2? 如何在发布/获取中获取多选下拉列表值的所有选定值? - How to get all the selected values of multiselect dropdown value in post/get? 如何用PHP中的选定值填充Bootstrap Multiselect? - How to populate Bootstrap Multiselect with selected values in php? Bootstrap 表从选定行获取数据 - Bootstrap table get data from selected row 如何在多选表单上从数据库中回显多选中的选定值 - How to echo selected values on multiselect from database on a multiselect form 如何从输入字段模态引导程序中获取值并使用 jquery 将值显示到同一模态中的另一个输入字段? - How to get value from input field modal bootstrap and show the value to another input field in the same modal with jquery? 我如何返回数组并在数据库的其他类中使用它 - How could I return array and use it in anoher class from database 如何从同一卷获取一列中的总和值并使用codeigniter显示所有行? - How to get sum value in a column from same roll and show all row using codeigniter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM