如何添加输入值

How to add input values

本文关键字:输入 添加 何添加      更新时间:2023-11-23

我有4个文本框,即customer_phy_totcustomer_che_totcustomer_bio_tot。我正在尝试将所有3个输入框的值添加并显示到第四个输入customer_pcb_tot中。

customer_bio_obt.blur(function(){
    var customer_pcb_tot = isNaN(parseInt($("#customer_phy_tot").val() + $("#customer_che_tot").val() + $("#customer_bio_tot").val())) ? 0 :( $("#customer_phy_tot").val() + $("#customer_che_tot").val() + $("#customer_bio_tot").val())
    $("#customer_pcb_tot").val(customer_pcb_tot);
});

问题是,与其添加我的代码,不如将其视为字符串。假设我的值分别为10155。它应该在第4个框中显示30,但在我的情况下,它显示的是10155

非常感谢您的帮助。

您已经在使用parseInt(),但您需要在每个单独的val()调用返回的字符串值上使用它。还要注意,使用三元表达式是多余的。试试这个:

customer_bio_obt.blur(function() {
    var customer_pcb_tot = parseInt($("#customer_phy_tot").val(), 10) + parseInt($("#customer_che_tot").val(), 10) + parseInt($("#customer_bio_tot").val(), 10);
    $("#customer_pcb_tot").val(customer_pcb_tot || 0);
});

使用parseInt()函数执行此操作:)

你必须将它用于你的每一个价值观,并使其发挥作用。

正如@Jamie R Rytlewski所说:

请确保也使用基数值。parseInt(字符串,基数);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

您可以在请求的每个val()中解析Int()。或者在求和之前将每一个乘以1。

var customer_pcb_tot=isNaN(parseInt($("#customer_phy_tot").val()+$("#customer_che_tot"?0:($("#customer_phy_tot").val()*1+$("#customer_che_tot"