如何添加输入值
How to add input values
我有4个文本框,即customer_phy_tot
、customer_che_tot
、customer_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);
});
问题是,与其添加我的代码,不如将其视为字符串。假设我的值分别为10
、15
和5
。它应该在第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"
相关文章:
- 为具有相同类的多个输入添加、移除和添加一个类
- 为收音机点击的输入添加值,然后删除
- 单击向输入添加文本值的链接
- 为数字输入添加特殊字符
- 敲除-将用户输入添加到表中的问题
- 只选中一个复选框,然后为选中的输入添加类
- 将 html 输入添加到表中
- 将表单中的输入添加到同一页面中的表中,而无需重新加载
- 将用户输入添加到数组中并在单击时显示
- 显示输入,然后将输入添加到剪贴板ie11
- Javascript:根据用户输入添加到静态URL;添加#xyz
- 通过jquery为mvc项目中动态添加的输入添加验证规则
- 未将标准输入添加到Angularjs Form$Scope中
- jQuery在单击时向输入添加文本
- 当使用AngularJS将新输入添加到另一个输入字段时,如何清除输入字段中的值
- 复选框将输入添加到文本框总选择和总选择成本
- Blueimp为多个输入添加了上传触发器
- 在JavaScript中单击按钮时将用户输入添加到列表
- 表单:如何为新添加的输入添加验证规则
- 通过单击向输入添加值