最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to add 2 values of textbox in asp.net using javascript - Stack Overflow

programmeradmin0浏览0评论

I have 2 textbox in my asp page and also have one hiddenfield in my asp 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

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

can anybody suggest me what is wrong in my code and what is the right way to get desired value

I have 2 textbox in my asp page and also have one hiddenfield in my asp 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

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

can anybody suggest me what is wrong in my code and what is the right way to get desired value

Share Improve this question asked Jan 2, 2009 at 14:28 RBSRBS 3,87111 gold badges37 silver badges33 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3
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

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;
发布评论

评论列表(0)

  1. 暂无评论