如何在表单字段中显示JavaScript数据?
var amzntheURL = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=AMZN&apikey=demo";
$(document).ready(function() {
$("#amznstockIndicator").show();
doAjax(amzntheURL);
$('.ajaxtrigger').click(function() {
$("#amznstockIndicator").show();
doAjax(amzntheURL);
return false;
});
function amznnumberWithCommas(x) {
return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
}
function doAjax(url) {
$.ajax({
url: url,
dataType: 'json',
contentType: "application/json",
success: function(data) {
var amznsymbol = data['Global Quote']['01. symbol']
var amznlastTradePriceOnly = data['Global Quote']['05. price']
var amznlastVolume = data['Global Quote']['06. volume']
var amznchangep = data['Global Quote']['10. change percent']
$('#DataID').html(amznlastTradePriceOnly);
$("#amznstockIndicator").hide();
}
});
}
});
<div>
<span id="DataID"></span>
</div>
<form role="form" action="{{url('user/name')}}" method="post">
<div class="form-group mb-3">
<div class="input-group input-group-merge input-group-alternative">
<div class="input-group-prepend">
<span class="input-group-text"> USD</span>
</div>
<input type="number" step="any" class="form-control" placeholder="" name="amount" required>
<input type="hidden" class="form-control" value="AMZN" name="symbol">
<input type="hidden" class="form-control" id="DataID" name="price">
</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary my-4">Purchase</button>
<h4><span id="amznstock_changp"></span></h4>
</div>
</form>
当我键入代码然后在页面上显示数据时,它工作正常,但是我希望在表单字段中显示相同的数据。 如有任何疑问,请告诉我,然后我将更改文本。