如何检索旧文本并从总数中减去 - JS

时间:2017-11-24 16:53:02

标签: javascript jquery

这是关于现有问题的后续问题。我能够获得我选择的总分价格产品并加起来以获得总计。现在,当取消选择某个项目时,我想从现有的总计中减去要取消选择的项目的价格?

我如何完成这项工作?

当我试图获得oldtext时,它总是为0 ..为什么会发生这种情况?

HTML

<div class="panel" id="panel">

<div>


<div >
<p>   class="mygoods" >Total: <span  ></span></p> 
</div>

JS

     <script type="text/javascript">

        function order(food)
        { 

            var ad = JSON.parse(food.dataset.food);    
              if(food.checked == true)  {
            $('.panel').append(
                '<div class="container" style=" font-size:14px; "> '+ 
    '<p class="total" ><span class="sub-total" name="total" id="total"></span></p>'+
'<input size="50" type="text" class="form-control quantity" id="qty" placeholder=" qty " name="quantity[]"  required/>'+


             '</div>'   
              )


            }
            else{
                var total = $(".panel .container [data-id=" + ad.id + "]").parent().find(".total").text();
                $(".panel .container [data-id=" + ad.id + "]").parent().remove();

            if (total) {
                $('.mygoods span').text(function(oldtext) {
                    console.log('this is my old text '+oldtext)
                    return oldtext ? oldtext - total : oldtext;

                });
            }                 
                }         
        }




        $('.panel').on('keyup','.quantity',function()

               {
                var sum;


                container = $(this).closest('div');

               quantity = Number($(this).val());
                price = Number($(this).closest('div').find('.price').data('price'));    

               container.find(".total span").text(quantity * price);

                sum = 0;



               $(".sub-total").each(function(){
               sum = sum + Number($(this).text());
               })                

               $('.mygoods span').text(sum);

               });

            </script>

1 个答案:

答案 0 :(得分:1)

  $( '.mygoods span' ).text( function( oldtext ) {
    console.log( 'this is my old text ' + oldtext )
    return oldtext ? oldtext - total : oldtext;
  } );

.text方法返回参数indextext - 您只需检索一个参数。因此,index存储在oldtext变量中,而不是文本中。

  

类型:函数(整数索引,字符串文本)=&gt; String一个函数   返回要设置的文本内容。收到的指数位置   集合中的元素和作为参数的旧文本值。   http://api.jquery.com/text/

您只需添加其他参数即可解决此问题。

$( '.mygoods span' ).text( function(index, oldtext ) {
console.log( 'this is my old text ' + oldtext )
return oldtext ? oldtext - total : oldtext;
} );

我尝试复制一个代码段来向您展示,但您提供的代码不足以构建任何内容。尝试构建如下。

&#13;
&#13;
function order( food ) {
  var ad = JSON.parse( food.dataset.food );
  if ( food.checked == true ) {
    $( '.panel' ).append( '<div class="container" style=" font-size:14px; "> ' +
      '<p class="total" ><span class="sub-total" name="total" id="total"></span></p>' +
      '<input size="50" type="text" class="form-control quantity" id="qty" placeholder=" qty " name="quantity[]"  required/>' +
      '</div>' )
  } else {
    var total = $( ".panel .container [data-id=" + ad.id + "]" ).parent().find(
      ".total" ).text();
    $( ".panel .container [data-id=" + ad.id + "]" ).parent().remove();
    if ( total ) {
      $( '.mygoods span' ).text( function( index,  oldtext ) {
        console.log( 'this is my old text ' + oldtext )
        return oldtext ? oldtext - total : oldtext;
      } );
    }
  }
}
$( '.panel' ).on( 'keyup', '.quantity', function() {
  var sum;
  container = $( this ).closest( 'div' );
  quantity = Number( $( this ).val() );
  price = Number( $( this ).closest( 'div' ).find( '.price' ).data( 'price' ) );
  container.find( ".total span" ).text( quantity * price );
  sum = 0;
  $( ".sub-total" ).each( function() {
    sum = sum + Number( $( this ).text() );
  } )
  $( '.mygoods span' ).text( sum );
} );
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel" id="panel">
    <div>
      <div>
        <p class="mygoods"> Total: <span></span></p>
      </div>
    </div>
  </div>
&#13;
&#13;
&#13;