View Full Version : جمع مجموعه ای از textbox
karimi84
یک شنبه 21 اسفند 1384, 20:01 عصر
سلام
من javascript خیلی بلد نیستم ولی در برنامه ای جمع مجموعه ای از textbox ها را بوسیله جاوا اسکریپت نوشته شده است ولی خروجی ان که در یک textbox دیگر نمایش داده میشود مقادیر textbox ها را جمع جبری نمی کند بلکه همه انها را کنار هم قرار میدهد برای حل این مشکل اگر چه کار باید بکنم .خواهشمندم در صورت امکان نمونه کدی در این باره برایم قرار دهید
باتشکر فراوان ( فقط خیلی سریع )
iiiiii
جمعه 11 فروردین 1385, 07:56 صبح
shoma bayad value ha ra be ADDAD tabdil konid
ba estefade az parseInt ya parseFloat
var result = parseInt(textfield1.value) + parseInt(textfield2.value);
parseFloat function
Parses a string argument and returns a floating point number.
Syntax
parseFloat(string)
string is a string that represents the value you want to parse.
Description
The parseFloat function is a built-in JavaScript function. It is not a method associated with any object, but is part of the language itself.
parseFloat parses its argument, a string, and returns a floating point number. If it encounters a character other than a sign ( + or -), numeral (0-9), a decimal point, or an exponent, then it returns the value up to that point and ignores that character and all succeeding characters.
If the first character cannot be converted to a number, parseFloat returns one of the following values:
0 on Windows platforms.
"NaN" on any other platform, indicating that the value is not a number.
For arithmetic purposes, the "NaN" value is not a number in any radix. You can call the isNaN function to determine if the result of parseFloat is "NaN". If "NaN" is passed on to arithmetic operations, the operation results will also be "NaN".
Examples
The following examples all return 3.14:
parseFloat("3.14")
parseFloat("314e-2")
parseFloat("0.0314E+2")
var x = "3.14"
parseFloat(x)
The following example returns "NaN" or 0:
parseFloat("FF2")
See also
isNaN, parseInt functions
--------------------------------------------------------------------------------
parseInt function
Parses a string argument and returns an integer of the specified radix or base.
Syntax
parseInt(string [,radix])
string is a string that represents the value you want to parse.
radix is an integer that represents the radix of the return value.
Description
The parseInt function is a built-in JavaScript function. It is not a method associated with any object, but is part of the language itself.
The parseInt function parses its first argument, a string, and attempts to return an integer of the specified radix (base). For example, a radix of 10 indicates to convert to a decimal number, 8 octal, 16 hexadecimal, and so on. For radixes above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through F are used.
If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. ParseInt truncates numbers to integer values.
If the radix is not specified or is specified as 0, JavaScript assumes the following:
If the input string begins with "0x", the radix is 16 (hexadecimal).
If the input string begins with "0", the radix is 8 (octal).
If the input string begins with any other value, the radix is 10 (decimal).
If the first character cannot be converted to a number, parseFloat returns one of the following values:
0 on Windows platforms.
"NaN" on any other platform, indicating that the value is not a number.
For arithmetic purposes, the "NaN" value is not a number in any radix. You can call the isNaN function to determine if the result of parseInt is "NaN". If "NaN" is passed on to arithmetic operations, the operation results will also be "NaN".
Examples
The following examples all return 15:
parseInt("F", 16)
parseInt("17", 8)
parseInt("15", 10)
parseInt(15.99, 10)
parseInt("FXX123", 16)
parseInt("1111", 2)
parseInt("15*3", 10)
The following examples all return "NaN" or 0:
parseInt("Hello", 8)
parseInt("0x7", 10)
parseInt("FFF", 10)
Even though the radix is specified differently, the following examples all return 17 because the input string begins with "0x".
parseInt("0x11", 16)
parseInt("0x11", 0)
parseInt("0x11")
pooyanm
جمعه 11 فروردین 1385, 11:55 صبح
سلام پست قبلی کامل نبود!
احتمالا شما مقدار textfield ها را به عدد تبدیل نمی کنید. این کد باید مشکل شما را حل کند.
اگر که مشکل حل نشد همین جا کد برنامه تان را بنویسید تا آن را بررسی کنم
pooyanc@yahoo.com
<html>
<body>
<h2>Add These Numbers</h2>
<br>
<br>
First Number:
<input type=text id=num1 value="2" onKeyUp="add();">
<br>
Second Number:
<input type=text id=num2 value="2" onKeyUp="add();">
<br>
<br>
Result:
<input type=text id=floatResult onKeyPress="add();">
<script type="text/javascript">
function add(){
var num1 = document.getElementById('num1');
var num2 = document.getElementById('num2');
var floatResult = document.getElementById('floatResult');
floatResult.value = parseFloat(num1.value) + parseFloat(num2.value);
}
add();
</script>
<body>
</html>
JavaScript Documentation
parseFloat function
Parses a string argument and returns a floating point number.
Syntax
parseFloat(string)
string is a string that represents the value you want to parse.
Description
The parseFloat function is a built-in JavaScript function. It is not a method associated with
any object, but is part of the language itself.
parseFloat parses its argument, a string, and returns a floating point number. If it encounters
a character other than a sign ( + or -), numeral (0-9), a decimal point, or an exponent, then it
returns the value up to that point and ignores that character and all succeeding characters.
If the first character cannot be converted to a number, parseFloat returns one of the following
values:
0 on Windows platforms.
"NaN" on any other platform, indicating that the value is not a number.
For arithmetic purposes, the "NaN" value is not a number in any radix. You can call the isNaN
function to determine if the result of parseFloat is "NaN". If "NaN" is passed on to arithmetic
operations, the operation results will also be "NaN".
Examples
The following examples all return 3.14:
parseFloat("3.14")
parseFloat("314e-2")
parseFloat("0.0314E+2")
var x = "3.14"
parseFloat(x)
The following example returns "NaN" or 0:
parseFloat("FF2")
See also
isNaN, parseInt functions
--------------------------------------------------------------------------------
parseInt function
Parses a string argument and returns an integer of the specified radix or base.
Syntax
parseInt(string [,radix])
string is a string that represents the value you want to parse.
radix is an integer that represents the radix of the return value.
Description
The parseInt function is a built-in JavaScript function. It is not a method associated with any
object, but is part of the language itself.
The parseInt function parses its first argument, a string, and attempts to return an integer of
the specified radix (base). For example, a radix of 10 indicates to convert to a decimal
number, 8 octal, 16 hexadecimal, and so on. For radixes above 10, the letters of the alphabet
indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through
F are used.
If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and
all succeeding characters and returns the integer value parsed up to that point. ParseInt
truncates numbers to integer values.
If the radix is not specified or is specified as 0, JavaScript assumes the following:
If the input string begins with "0x", the radix is 16 (hexadecimal).
If the input string begins with "0", the radix is 8 (octal).
If the input string begins with any other value, the radix is 10 (decimal).
If the first character cannot be converted to a number, parseFloat returns one of the following
values:
0 on Windows platforms.
"NaN" on any other platform, indicating that the value is not a number.
For arithmetic purposes, the "NaN" value is not a number in any radix. You can call the isNaN
function to determine if the result of parseInt is "NaN". If "NaN" is passed on to arithmetic
operations, the operation results will also be "NaN".
Examples
The following examples all return 15:
parseInt("F", 16)
parseInt("17", 8)
parseInt("15", 10)
parseInt(15.99, 10)
parseInt("FXX123", 16)
parseInt("1111", 2)
parseInt("15*3", 10)
The following examples all return "NaN" or 0:
parseInt("Hello", 8)
parseInt("0x7", 10)
parseInt("FFF", 10)
Even though the radix is specified differently, the following examples all return 17 because the
input string begins with "0x".
parseInt("0x11", 16)
parseInt("0x11", 0)
parseInt("0x11")
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.