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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.