Javascript 使用JS计算值并将该值存储在数据库中
我用js计算2的值:Javascript 使用JS计算值并将该值存储在数据库中,javascript,laravel-5.8,Javascript,Laravel 5.8,我用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').
$('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>
@csrf
我的问题是:如何将此计算值存储在DB中?您的代码在这行中有语法错误:
document.getElementById('user_receive_fund_amount').value = firstValue * secondValue ;});
删除;})代码>从末尾开始。因此,这条线变成:
document.getElementById('user_receive_fund_amount').value = firstValue * secondValue;
您可以使用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>
$(文档).ready(函数(){
$('#用户\发送\资金\金额')。更改(函数(){
var firstValue=parseInt($('#user_send_fund_amount').val())|0;
var secondValue=parseInt($('固定金额').val())| 0;
$(“#用户(接收)资金(金额)”).val(firstValue*secondValue);
});
});
@csrf
只需进行此更改!希望这能奏效:
$(“输入[name=user\u receive\u fund\u amount]”).val(第一个值*第二个值)代码>您的意思是存储在数据库中吗?使用本地存储还是会话存储?等等@justDan Yes store in database您使用的是哪种数据库?@kinggray using phpmyadminphmyadmin不是一个数据库-它是一个管理MySQL数据库的工具,但仍然无法存储该计算值。在同一篇文章中检查我的修改