本文介绍了jQuery的-填写复选框/收音机/下拉菜单后计算总价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三个部分的付款表格.基本上,第一个是带有乘法器的无线电"列表,第二个是带有设置值的复选框"列表,第三个是也带有乘法器的下拉列表==> (jsfiddle).为了清楚起见,我插入了一个文本框,其中将显示总的计算值.

I have a payment form with three sections. Basically, the first is a "radio" list with multipliers, the second is a "checkbox" list with set values, and the third is a dropdown list with multipliers as well ==> (jsfiddle). For clarity, I inserted a text box that would display the total calculated value.

用户填写此表格时,如何计算和显示总数?

When users fill out this form, how do I calculate and display the total?

我尝试使用堆栈溢出响应,但根本不起作用.

I tried using this stack overflow response, but it does not work at all.

$('input').change(function(){
    var tot = 1;
    $.each($('input'),function(){
      var curr_val = $(this).val();
        if(curr_val != ""){
          tot = tot * curr_val;
          $('#total').val(tot);
        }
    });
});

推荐答案

您可以将选择器更改为排除 #total,因为您不想听更改-$(':input').not('#total').change

You can change your selector to exclude the #total as you don't want to listen to its change - $(':input').not('#total').change.

您可以使用 #map() jquery函数来获取选中框.

下一步是使用 #reduce() 函数来添加数组.

The next step would be to multiply them all - using #reduce() function to add the array.

请参见下面的演示

$(':input').not('#total').change(function() {
  var tot = 1;

  var s1 = +$('input[name=section1]:checked').val();
  var s2 = $('input[name=section2]:checked').map(function() {
    return +$(this).val();
  }).get();
  var s3 = +$('select').val();
  $('#total').val(s1 * (s2.reduce(function(p,c){
    return p + c;
  },0) || 1) * s3);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" action="">
  <div id="section1">Section 1
    <br><input type="radio" name="section1" value="2"> x2
    <br><input type="radio" name="section1" value="1.5"> x1.5
    <br><input type="radio" name="section1" value="1.5"> x1.5
    <br><input type="radio" name="section1" value="1.5"> x1.5
    <br><input type="radio" name="section1" value="1"> x1
    <br><input type="radio" name="section1" value="1"> x1
  </div>
  <br>
  <div id="section2">Section 2
    <br><label><input type="checkbox" name="section2" value="4"><span>1</span></label>
    <label><input type="checkbox" name="section2" value="4"><span>2</span></label>
    <label><input type="checkbox" name="section2" value="4"><span>3</span></label>
    <label><input type="checkbox" name="section2" value="4"><span>4</span></label>
    <label><input type="checkbox" name="section2" value="4"><span>5</span></label>
    <label><input type="checkbox" name="section2" value="4"><span>6</span></label>
    <br><label><input type="checkbox" name="section2" value="4"><span>7</span></label>
    <label><input type="checkbox" name="section2" value="4"><span>8</span></label>
    <label><input type="checkbox" name="section2" value="4"><span>9</span></label>
    <label><input type="checkbox" name="section2" value="4"><span>10</span></label>
    <label><input type="checkbox" name="section2" value="4"><span>11</span></label>
    <label><input type="checkbox" name="section2" value="4"><span>12</span></label>
  </div>
  <br>
  <div>Section 3<br>
    <select>
   <option value="7">1 week</option>
   <option value="14">2 weeks</option>
   <option value="21">3 weeks</option>
   <option value="28">4 weeks</option>
  </select>
  </div>
  <input type="text" value="0" id="total">
</form>

这篇关于jQuery的-填写复选框/收音机/下拉菜单后计算总价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 00:14