开发者

After omitting $ from input getting NaN in javascript asp.net

开发者 https://www.devze.com 2023-03-15 02:01 出处:网络
In javascript, I have given开发者_StackOverflow社区 like this,( edited adding Pasman response ) var y= document.getElementById(\'<%= DataItemValue4.ClientID%>\').firstChild.nodeValue.replace(\'

In javascript, I have given开发者_StackOverflow社区 like this,( edited adding Pasman response )

var y= document.getElementById('<%= DataItemValue4.ClientID%>').firstChild.nodeValue.replace('$', '');
alert(Number(y));
y = y *48;

the value I am reading here is integer type amount with $, ex: $200, $10 etc., and after omitting $ symbol, i get ex: 200, 10. Now before i do arithmetic operations on y, alert shows me value as NaN,even if i don't write Number() also i am getting same problem. how to get numerical y and able to perform arithmetic operation on it.


You can replace the $ sign like this:

var y = document.getElementById('<%= DataItemValue4.ClientID%>')
                .firstChild.nodeValue.replace('$','');


If the string is always exactly '$xxx' (i.e. no spaces etc), then you just take the substring (with e.g. substr):

val = val.substr(1);

If there are spaces, you have to use regular expressions (basically trimming the value):

val = val.replace(/^[\s$]+|[\s]+$/g, '');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号