【问题标题】:jQuery checkboxes count total valuejQuery复选框计算总值
【发布时间】:2015-11-19 15:22:59
【问题描述】:

我要做的是显示每个选定复选框的总数,例如:如果我选择一个值为 100 的复选框,然后选择另一个值为 200 的复选框,我会得到 300 作为输出,如果我取消选择它会去总。我尽我所能,但根本没有结果,需要一些帮助。

这是上面的html 我使用 PHP,所以我可以更快地更改值

<?php 
    //basisprijs en titel  die erbij komen, Dit moet aangepast worden.
    $basisPrijs=
    array(
        array("titel" => 'template1', "prijs" => '100'),
        array("titel" => 'template2', "prijs" => '200'),
        array("titel" => 'template3', "prijs" => '300'),
        array("titel" => 'template4', "prijs" => '400')
    );
    //onderdelen en titel die erbij komen, Dit moet aangepast worden.
    $onderdelen=
    array(
        array("titel" => 'Table', "prijs" => '100'),
        array("titel" => 'Checkbox', "prijs" => '200'),
        array("titel" => 'List', "prijs" => '300'),
        array("titel" => 'Selectbox', "prijs" => '400')
    );
    $titel="Bereken tool";
    $check="Bestellen";
    ?>   

这是在头部和Jquery之后

 <body>
  <!-- <script type="text/javascript"></script> -->
  <div class="container">
    <div id="content">
      <h3><?php echo $titel; ?></h3>
      <form action="index.php" method="post">
        <div class="checkbox">
           <?php foreach($onderdelen as $key => $value)
           {echo "<div class='checkbox'>
           <label><input class='check' type='checkbox' value='".$onderdelen[$key]["prijs"]."' 
            name='".$onderdelen[$key]["titel"]."'>"
            .$onderdelen[$key]["titel"]."</label></div>"
                        ;}?>
          </div>
        </form>
      <div>
      <button type="button" class="btn btn-default"><?php echo $check; ?></button>
      <p id="output">output:</p>
    </div>
  </div>
</div> 
<script>
  $(document).ready(function() { 
            $(".check").change(function() {
               var prijs= this.checked ? this.value : '';
               $("#output").text(prijs); 
            });
        });   
 </script>

【问题讨论】:

    标签: javascript php jquery checkbox


    【解决方案1】:

    试试这个(我没试过,但就是这个概念):

    <script>
        $(document).ready(function()
        { 
           $('.check').change(function()
           {
               var totalPrijs=0;
               $('.check').each(function () {
                   if (this.checked) {
                       totalPrijs+=parseInt($(this).val());
                   }
               });
    
               $("#output").text(totalPrijs); 
           });
    
        });   
    </script>
    

    【讨论】:

      【解决方案2】:

      您也可以通过附加change 侦听器来侦听包装div 上的复选框更改。 在处理程序中,将total 设置为零,并遍历所有带有.check 类的检查输入,按值增加total,最后显示结果。

      $('.checkbox').on('change', function () {
          var total = 0;
          $('.check:checked').val(function (idx, val) { // Iterates checked inputs only
              total += +val;
              return val;
          });
          $('#result').text(total);
      });
      

      A working demo at jsFiddle.

      那些需要多个复选框字段的人,可以使用这个稍微修改的代码:

      function countTotal () {
          var total = 0;    
          $('#' + this.id + ' .check:checked').val(function (idx, val) {
              total += +val;
              return val;
          });
          $('#res' + this.id).text(total);
      }
      
      $('.checkbox').on('change', countTotal);
      

      A working demo at jsFiddle.

      注意,现在包装 divs 需要一个 id。

      【讨论】:

        【解决方案3】:

        试试这个,

        $(document).ready(function(){
        
                $(".check").change(function(){
                    var tot = 0;
                    $(".check:checked").each(function(){
                       tot += parseInt($(this).val());              
                    });
                    $("#output").text(tot);
                });
        });
        

        【讨论】:

          猜你喜欢
          • 2011-07-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-19
          • 2018-02-05
          • 2013-12-17
          相关资源
          最近更新 更多