gpt4 book ai didi

javascript - jQuery - asp.net mvc- 字段不接受数字序列中多个逗号 - float 字

转载 作者:行者123 更新时间:2023-12-02 23:17:47 26 4
gpt4 key购买 nike

例如,如果我输入5,000,系统会识别为5,00

发生的另一个问题是,如果我输入,例如 5,100,系统会识别为 null 并将该值保存为 0.00

我尝试了一切方法来理解这个问题,但我做不到

如何让字段识别用逗号或点输入的值?

我的代码:

public class EstoqueDaEmbarcacao : Entity
{
public decimal AguaCarregada { get; set; }

public decimal DieselCarregado { get; set; }
}

jQuery

function fillInventoryData(barcoId) {
$("#estoque-barco-id").val(barcoId);
$("#estoque-barco-nome").val($("#selectEmbarcacao option:selected").text());
$("#modal-estoque").modal('show');
}
function saveInventory() {
$("#message-estoque").removeClass("alert-danger");
$("#message-estoque").removeClass("alert-warning");
var barcoId = $("#estoque-barco-id").val();
var agua = $("#estoque-barco-agua").val().replace(",", ".");
var diesel = $("#estoque-barco-diesel").val().replace(",", ".");
var data = JSON.stringify({ AguaCarregada: agua, DieselCarregado: diesel, BarcoId: barcoId });
if (agua == "" || diesel == "") {
$("#message-estoque").addClass("alert-warning");
$("#message-estoque").html("Fill in all fields to continue");
return;
}
$.ajax({
url: "/InfoApontamento/AtualizarEstoqueBarco",
type: "POST",
dataType: "json",
data: data,
contentType: "application/json",
success: function (result) {

if (!result.Success) {

$("#message-estoque").html(result.ErrorDatail);
$("#message-estoque").addClass("alert-danger");
}
else if (!result.Data) {
$("#message-estoque").html(result.Message);
$("#message-estoque").addClass("alert-danger");
}
else {
alert("Salvo com sucesso!");
$("#modal-estoque").modal('hide');
$('#estoque-barco-agua').val("");
$('#estoque-barco-diesel').val("");
// abrirModalManutencao();
preInicializarModal();
}
}
});

}

页面:

<div class="form-row">
<div class="col">
<label for="embarcacaoInput">Água</label>
<input type="number" step="0.01" class="form-control" id="estoque-barco-agua">
</div>
<div class="col">
<label for="embarcacaoInput">Diesel</label>
<input type="number" step="0.01" class="form-control" id="estoque-barco-diesel">
</div>
</div>

最佳答案

更改此行:

var data = JSON.stringify({ AguaCarregada: agua, DieselCarregado: diesel, BarcoId: barcoId });

为此(parseFloat()):

var data = JSON.stringify({ AguaCarregada: parseFloat(agua), DieselCarregado: parseFloat(diesel), BarcoId: barcoId });

关于javascript - jQuery - asp.net mvc- 字段不接受数字序列中多个逗号 - float 字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57101269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com