problem summary
I'm working on adding different numbers to the areas based on the code> Value of input boxes . I am currently experiencing this problem in which the jQuery code is adding the value of
Further details
Here I am using HTML:
& lt; Form action = "method =" post "& gt; ; & Lt; Input type = "text" id = "one" value = "20.00" /> & Lt; Input type = "text" id = "two" value = "10.00" /> & Lt; A href = "#" id = "add" & gt; Add field & lt; / A & gt; & Lt; Input type = "submit" value = "submit" /> & Lt; / Form & gt;
Here's my jQuery (this behavior was described above with this script):
$ (function () {var one = $ ('#'). Val (), two = $ ('# two'). Val (); $ ('# add'). Click (function (e) {e.preventDefault; var three = one + Two; alert (three);});});
This clearly happened in the output:
20.0010.00
So I sent my first variable declarations Try to modify parseInt ()
as:
var a = parseInt ($ ('# one'). Val (), 10), two = ParseInt ($ ('#two'), (value), 10).
Anytime results in:
Nain
So I tried to get the value first and Then Val (), two = $ ('# two'). Val (), i_one = :
var a = $ ('# one' parseInt (a), i_two = parseInt (two);
But so far the agan ... was the result of
I used to use the above
parseFloat ()
Has resulted in the same unfortunate result.
I have also tried (read somewhere on a blog) which by adding +
to further jQuery The variable will be bound to treat it as an integer, so I saw (h I got the one
and two
above):
u_one = + a u_two = + 2
< P> I'm starting to think that getting value using val ()
prevents them from being used as something other than strings ... but I should be wrong. How can you get these values in the integer format, please advise on it so that I can get results:
30.00
When the field Added Is it?
Favorites & lt; Input / & gt; Keeping
and hiding another not adding, I can start text
thanks for reading
note
This problem has come to light that this problem was not related to jQuery and was related to the template I was using above the code mentioned above in the comments given below as a reply to the code, but all jQuery Will post examples.
Try this way
$ (function () { Var one = $ ('# a'). Val (); var two = $ ('# two'). Val (); $ ('# add'). Click (function (e) {e.preventDefault; Var three = parseint (one) + parseint (two); alerts (three);});});
View the demo at jsfiddle:
Comments
Post a Comment