简体   繁体   English

如何使用JavaScript在asp.net中添加文本框的2个值

[英]how to add 2 values of textbox in asp.net using javascript

I have 2 textbox in my asp.net page and also have one hiddenfield in my asp.net page , my hiddenfield will always have numeric value like 123.00 , and in my one textbox also I will always have numeric value like 20.00 now I want to add this hiddenfield value and textbox value and display it into second textbox thru javascript 我在asp.net页面上有2个文本框,在asp.net页面上也有一个hiddenfield,我的hiddenfield总是具有数字值,如123.00,而在我的一个文本框中,我也总是具有数字值,如20.00现在我想添加此hiddenfield值和文本框值,并通过javascript将其显示到第二个文本框

I wrote the following code to do this 我写了下面的代码来做到这一点

var amt = document.getElementById("txtsecond");
    var hiddenamt = document.getElementById("AmtHidden").value
    var fee = document.getElementById("txtFirst").value;
    amt.value = hiddenamt + fee; 

this should give me result like 123.00+20.00 = 143.00 but this is concatnating hiddenamt value and fee value and giving me result like 12320.00 in my first textbox 这应该给我类似123.00 + 20.00 = 143.00的结果,但这是隐藏的价格和费用值的结合,并且在我的第一个文本框中给我的结果像12320.00

can anybody suggest me what is wrong in my code and what is the right way to get desired value 任何人都可以建议我代码中的错误以及获得期望值的正确方法是什么

amt.value = parseFloat(hiddenamt) + parseFloat(fee);

the value of an input is just a string - convert to float parseFloat(foo) in JS and you'll be fine 输入的值只是一个字符串-在JS中转换为float parseFloat(foo) ,就可以了

edited to make float as I notice it's probably important for you 修改为浮动,因为我注意到这对您可能很重要

Textboxes are strings, you need to convert from a String to a Number: 文本框是字符串,您需要将字符串转换为数字:

var hiddenamt = parseFloat(document.getElementById("AmtHidden").value);
var fee = parseFloat(document.getElementById("txtFirst").value);

Eric 埃里克

you should parse the values to decimals first: 您应该先将值解析为小数:

decimal amt, hiddenamt, fee;
Decimal.TryParse(document.getElementById("txtsecond"),out amt);
Decimal.TryParse(document.getElementById("txtfirst"),out fee);
hiddenamt = amt + fee;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM