【问题标题】:Calculating a value with JS and Storing that value in database用 JS 计算一个值并将该值存储在数据库中
【发布时间】:2020-02-04 14:13:17
【问题描述】:

我正在用 js 计算 2 个值:

$('input').keyup(function(){ // run anytime the value changes
    var firstValue  = Number($('#user_send_fund_amount').val());
    var secondValue = Number($('#fixed_amount').val());
    document.getElementById('user_receive_fund_amount').value = firstValue * secondValue ;
document.getElementById('user_receive_fund_amount').setAttribute('readOnly', true);
document.getElementById('user_receive_fund_amount').removeAttribute('readOnly');`

并尝试存储此值:

<form  method="POST" class="form-register" action="{{ route('order.store') }}">
 @csrf
   <input id="user_send_fund_amount" type="number" class="form-control"
     name="user_send_fund_amount" required>
    <input type="number" value="{{$receive->buy}}" class="form-control"
        id="fixed_amount" hidden>
    <input type="number" class="form-control" id="user_receive_fund_amount" name="user_receive_fund_amount"/>
</form>

我的问题是:如何将这个计算值存储在数据库中?

【问题讨论】:

  • 您的意思是存储在数据库中吗?使用本地存储还是会话存储?等等。
  • @justDan 是存储在数据库中
  • 你用的是什么数据库?
  • @KingGary 使用 phpmyadmin
  • phpmyadmin 不是数据库——它是管理 MySQL 数据库的工具

标签: javascript laravel-5.8


【解决方案1】:

只需进行此更改!并希望这会奏效: $("input[name=user_receive_fund_amount]").val(firstValue*secondValue);

【讨论】:

    【解决方案2】:

    您可以使用 JQuery 来计算如下代码中的值。

    <script>
    $(document).ready(function() { 
      $('#user_send_fund_amount').change(function(){
          var firstValue  = parseInt($('#user_send_fund_amount').val()) | 0;
          var secondValue = parseInt($('#fixed_amount').val()) | 0;
      $('#user_receive_fund_amount').val(firstValue * secondValue);
      });
    });
    </script>
    
    <form  method="POST" class="form-register" action="{{ route('order.store') }}">
     @csrf
       <input id="user_send_fund_amount" value="0" type="number" class="form-control"
         name="user_send_fund_amount" required>
        <input type="hidden" value="{{$receive->buy}}" class="form-control"
            id="fixed_amount">
        <input type="number" readonly class="form-control" id="user_receive_fund_amount" name="user_receive_fund_amount"/>
    </form>
    

    【讨论】:

    • 仍然无法存储该计算值。获取 null
    • 在同一篇文章中检查我的修改
    【解决方案3】:

    您的代码在这一行有语法错误:

    document.getElementById('user_receive_fund_amount').value = firstValue * secondValue ;});
    

    从末尾删除;})。所以这条线变成了:

    document.getElementById('user_receive_fund_amount').value = firstValue * secondValue;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多