【问题标题】:Need Help in adding div elements which have integer values在添加具有整数值的 div 元素时需要帮助
【发布时间】:2015-03-03 09:52:02
【问题描述】:

我有三个 DIV,其内容是整数值,并且经常从另一个来源更新。我的主要想法是将三个 div 的内容解析为 float 或 integer ,将它们相加并在另一个 div 中显示总数。我期待使用 onchange() 函数处理 div 中的内容,因为其中的内容会经常更改。以下是我的代码,它目前无法正常工作,如果您能帮我解决这个问题,我将不胜感激。 此 div 中的内容将使用文本输入经常更新,您可以创建一个文本输入输出来操作第一个 div,然后显示整个总和 提前致谢。

<script>
    function total() {
  var value1 = parseFloat($('#div1').innerHTML ()) || 0;
  var value2 = parseFloat($('#div2').innerHTML ()) || 0;
  var value3 = parseFloat($('#div1').innerHTML ()) || 0;
  var total;
 total=value1 + value2 + value3;
  $('#total').html(total);
}
  </script>  
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

</head>
<body >
  <div id="mywraper">
      <div id="div1" onchange="total()">
	    4
      </div>
      <div id="div2"  onchange="total()">
	    5
      </div>
	  <div id="div2" onchange="total()">
	    6
      </div>
</div>
 <div id="total_div">
    Total $<span id="total"></span>
  </div>
</body>
</html>

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

使用这个html()

<script>
    function total() {
  var value1 = parseFloat($('#div1').html()) || 0;
  var value2 = parseFloat($('#div2').html()) || 0;
  var value3 = parseFloat($('#div1').html()) || 0;
  var total;
 total=value1 + value2 + value3;
  $('#total').html(total);
}
  </script>  

【讨论】:

    【解决方案2】:

    试试这个:

     function total() {
        // fetch text using 'text' method and then convert string into number using '+' operator
        var value1 = +$('#div1').text() || 0;
        var value2 = +$('#div1').text() || 0;
        var value3 = +$('#div1').text() || 0;
        var total = value1 + value2 + value3;
        $('#total').html(total);
    }
    

    http://jsfiddle.net/uxajjk1b/2/

    【讨论】:

      【解决方案3】:

      使用text() 代替innerHTML,如下所示:

      <script>
          function total() {
            var value1 = parseFloat($('#div1').text()) || 0;
            var value2 = parseFloat($('#div2').text()) || 0;
            var value3 = parseFloat($('#div1').text()) || 0;
            var total;
            total=value1 + value2 + value3;
            $('#total').html(total);
          }
      </script>  
      

      【讨论】:

      • 请扩展您的答案,展示它如何以及为什么解决了问题提出的问题 - 这将在未来对其他人有所帮助,并且您更有可能得到点赞!
      【解决方案4】:

      我真的不想回答,因为这可能很难解决,因为我们一开始不知道值是如何更新的。然而,我最终做了一个相对广泛的例子,所以我们在这里。

      如前所述,onChange 需要用户输入或操作来检测任何更改。这意味着您的total() 只会在页面加载时触发一次(假设它位于&lt;/body&gt; 之前)。

      最好的方法是将total() 粘贴在更改 html 元素内部值的原始函数中。这样total()每次也会被触发。


      我忍不住让total() 更具活力。这样,如果您添加或删除这些子 div,则无需更新 javascript。

      Here's a link to the original jsfiddle

      var parentContainer = $('#mywraper');
      
      function total() {
      
          var values = {}; // Optional****
          var total = 0;
      
          // Loops through parent containers children ( in this case div elements ).
          parentContainer.children().text(function( i, val ) {
      
              var value = parseInt( val );
      
              // Creates a variable where the variable name is based on the current elements index and value is based on the text inside the element. 
              values[ 'child_' + (i+1) ] = value; // Optional****
      
              // Sums up all the values
              total += value;
      
          });
      
          // The optional lines enable you independently check each value, for example:
          // console.log( values.child_1 )
      
          // Push total into the #total element.
          $('#total').html( total );
      
      }
      
      total();
      

      这是一个使用点击事件更新值的示例。因此,您只需在点击事件中添加total()

      function total() {
      
        var parentContainer = $('#mywraper'),
            total = 0;
      
        parentContainer.children().text(function( i, val ) {
          total += parseInt( val );
        });
      
        $('#total').html( total );
      
      }
      
      total();
      
      $('#updateBtn').on("click", function() {
      
        $('#mywraper').children().text(function( i, val ) {
          return parseInt( val ) + 1;
        });
      
        total();
      
      });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      
      <div id="mywraper">
            <div>4</div>
            <div>5</div>
      	  <div>6</div>
      </div>
      <div id="total_div">
          Total $<span id="total"></span>
      </div>
      
      <button id="updateBtn">Update values</button>

      【讨论】:

        猜你喜欢
        • 2016-06-24
        • 2017-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-22
        • 2014-02-20
        • 2015-09-25
        相关资源
        最近更新 更多