Jump to content

multiply two numbers "on the fly" with ajax


web_master

Recommended Posts

hi,

 

I got a problem, I want to multiply two numbers "on the fly" with ajax. There is a two forms, the firs form is the quantity and the second is the price.

 

Example:

 

<input type="text" id="quantity_01" name="quantity_01" value="" /><input type="text" id="price_01" name="price_01" value="" />

<input type="text" id="quantity_02" name="quantity_02" value="" /><input type="text" id="price_02" name="price_02" value="" />

<input type="text" id="quantity_03" name="quantity_03" value="" /><input type="text" id="price_03" name="price_03" value="" />

...

 

so, after when I input the quantity_01 and price_01  value, I want to see the multiply summary of those two values (exmpl. 10 * 15,00 = 150,00), without any reload.

 

I'm a newbee in Ajax, so, please, give me some script, how can I do this?

 

In advanced thnxs

 

T

Why would you use Ajax for this? You can multiply numbers client side using JavaScript.

 

Do you know what Ajax is?

 

Ok, as I know the Ajax is a javascript - but I don't know the real differences... so if I can solve my problem with javascript, than I need the javascript script ...

 

thnxs

in meantime, i find a script, but with problem...

at first:

<head>

<script type="text/javascript" src="js/jquery.min.js"></script>

</head>...

 

<input type="text" id="quantity 01" name="quantity_01" value="" />

<input type="text" id="price 01" name="price_01" value="" />

<span class="res_01"> </span>

 

<script type="text/javascript">

$('input[id~=quantity], input[id~=price] ').keyup(function() {

var id = $(this).attr('id').split(' ');

$('.res_'+id[1]).html($('input[id="quantity '+id[1]+'"]').val() * $('input[id="price '+id[1]+'"]').val());

});

</script>

 

the problem is that is not a valid, because of space in id attribute. Is some way to change id attr. to be without space (quantity_01 or quantity-01...)?

 

thnxs

 

hi,

 

here is a partial solution of my problem:

 

<div class="tarto">

<input type="text" id="kom_1" name="quantity_01" value="" />

<input type="text" id="cena_1" name="price_01" value="" />

<span class="res_01"> </span>

</div>

 

<script type="text/javascript">

$('.tarto input').keyup(function() {

var id = $(this).attr('id').split('_');

$('.res_'+id[1]).html($('input[id="kom_'+id[1]+'"]').val() * $('input[id="cena_'+id[1]+'"]').val());

});

</script>

 

When I type (keyup) the values in id="kom_1" and id="price_01" I will see the multiple result in <span class="res_01">.

 

- The first problem is that I want to see result only in 2 decimals.

- The second problem is: after submit in place <span class="res_01"></span> I want to reload data from database, but if I modify values in "input" form I want to see the typed result...

 

I hope it is clear what I want (my english is not a best).

 

thnxs

 

T

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.