我如何解决这个jquery / ajax问题?

时间:2022-11-07 19:02:51

I do have 4 text inputs like this,

我有这样的4个文本输入,

<input type="text" name="amount" id="amount">
<input type="text" name="commission" id="commission">
<input type="text" name="discount" id="discount">
<input type="text" name="total_amount" id="total_amount">

Here I am calculating total_amount base on the values of #amount. To do this I am using ajax like this,

这里我根据#amount的值计算total_amount。要做到这一点,我使用这样的ajax,

var minlength = 2;
$("#amount").on('input', function () {
  var value  = $(this).val(), 
  if (value.length >= minlength ) {
    $.ajax({
      type: "POST",
      url: "includes/transactions/proccess_amount.php",
      data: {'amount':value},          
      success: function(response) {
        response = jQuery.parseJSON(response)
        $('#make_transaction')
          .find('[name="commission"]').val(response.commission).end()
          .find('[name="total_amount"]').val(response.total_amount).end(); 
      }    
    });
  } 
});

Now I want to change total_amount again if it has a value on #discount field. So I created a ajax request for #discount keyup and its working for me.

现在我想再次更改total_amount,如果它在#discount字段上有值。所以我为#discount keyup创建了一个ajax请求,它为我工作。

keyup script look like this.

keyup脚本看起来像这样。

var strLength = 1;
$("#discount").keyup(function () {
  var discount = $(this).val(),
      amount   = $('#amount').val(),
      comison  = $('#commission').val(),
      total    = $('#total_amount').val();

      if (discount.length >= strLength ) {
        $.ajax({
          type: "POST",
          url: "includes/transactions/proccess_discount.php",
          data: { 'amount':amount
                , 'comison': comison
                , 'total'   : total
                , 'discount': discount
                },

          success: function(response) {
            response = jQuery.parseJSON(response)
            $('#amount_dolar').val(''); 
            $('#make_transaction')
              .find('[name="amount_dolar"]').val(response.amount).end()
              .find('[name="total_amount"]').val(response.totalAfterDiscount).end();
          }

        });
      }
    }); 

My problem is I want to display previous total_amount when discount field is empty. At this stage it is always displaying the total with discount.

我的问题是我想在折扣字段为空时显示之前的total_amount。在这个阶段,它始终显示总折扣。

Update: Image with the output for better understanding. 我如何解决这个jquery / ajax问题?

更新:带有输出的图像以便更好地理解。

Can anybody tell how to do this? Thank you.

任何人都可以告诉你怎么做?谢谢。

3 个解决方案

#1


0  

I would suggest having the #amount ajax call again in the 'success' section of the response to the #discount call. The problem seems to be that the #amount ajax is calling proccess_amount.php and the #discount ajax is calling proccess_discount.php.

我建议在#discount调用的响应的“成功”部分再次调用#amount ajax。问题似乎是#amount ajax正在调用proccess_amount.php而#discount ajax正在调用proccess_discount.php。

#2


0  

The answer depends in what is in response.totalAfterDiscount

答案取决于response.totalAfterDiscount中的内容

Easy solution is to use the or operator ||

简单的解决方案是使用或运算符||

.val(response.totalAfterDiscount || response.amount)

BUT the problem with that is the fact if the totalAfterDiscount is zero (could be a 100% off sale) than it would show amount and not the discount.

但问题是,如果totalAfterDiscount为零(可能是100%折扣销售),而不是显示金额而不是折扣。

So next option depends on what totalAfterDiscount holds when there is no discount.

因此,下一个选项取决于没有折扣时totalAfterDiscount持有的内容。

If it holds an empty string

如果它持有一个空字符串

.val(response.totalAfterDiscount.length ?  response.totalAfterDiscount : response.amount)

If it is undefined

如果它是未定义的

.val(response.totalAfterDiscount!==undefined ? response.totalAfterDiscount : response.amount)

#3


0  

Keep the initial total amount in cookie on keyup of discount input where you are making ajax call and when discount gets null or empty, put that total account from cookie to total amount input.

在进行ajax调用的折扣输入的keyup中保留cookie中的初始总金额,当折扣变为null或为空时,将该总帐户从cookie添加到总金额输入。

#1


0  

I would suggest having the #amount ajax call again in the 'success' section of the response to the #discount call. The problem seems to be that the #amount ajax is calling proccess_amount.php and the #discount ajax is calling proccess_discount.php.

我建议在#discount调用的响应的“成功”部分再次调用#amount ajax。问题似乎是#amount ajax正在调用proccess_amount.php而#discount ajax正在调用proccess_discount.php。

#2


0  

The answer depends in what is in response.totalAfterDiscount

答案取决于response.totalAfterDiscount中的内容

Easy solution is to use the or operator ||

简单的解决方案是使用或运算符||

.val(response.totalAfterDiscount || response.amount)

BUT the problem with that is the fact if the totalAfterDiscount is zero (could be a 100% off sale) than it would show amount and not the discount.

但问题是,如果totalAfterDiscount为零(可能是100%折扣销售),而不是显示金额而不是折扣。

So next option depends on what totalAfterDiscount holds when there is no discount.

因此,下一个选项取决于没有折扣时totalAfterDiscount持有的内容。

If it holds an empty string

如果它持有一个空字符串

.val(response.totalAfterDiscount.length ?  response.totalAfterDiscount : response.amount)

If it is undefined

如果它是未定义的

.val(response.totalAfterDiscount!==undefined ? response.totalAfterDiscount : response.amount)

#3


0  

Keep the initial total amount in cookie on keyup of discount input where you are making ajax call and when discount gets null or empty, put that total account from cookie to total amount input.

在进行ajax调用的折扣输入的keyup中保留cookie中的初始总金额,当折扣变为null或为空时,将该总帐户从cookie添加到总金额输入。