By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,442 Members | 1,341 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,442 IT Pros & Developers. It's quick & easy.

calculation within a form

P: n/a
In my form, I have 4 objects that I want to work together:
<input name="price" type="text" id="price" value="100" size="4">

<input name="quantity" type="text" id="quantity" value="1" size="2">
<input name="shipping" type="radio" value="slow">

<input name="shipping" type="radio" value="fast">
<input name="total" type="text" id="total" size="8">
I 'd like that when someone checks the first radiobutton (slow),
the "total" textfield shows: price x quantity + 5

and when someone checks the second radiobutton (fast),
the "total" textfield shows: price x quantity + 10
Sadly, I don't know javascript, but there's got to be an easy way to do
that, right?

Thanks to all who will answer, any link to a page that has something
similar would also be appreciated.

Jul 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
I would do it with onClick

<input name="shipping" type="radio" value="slow" onClick="...">

elji wrote:
In my form, I have 4 objects that I want to work together:
<input name="price" type="text" id="price" value="100" size="4">

<input name="quantity" type="text" id="quantity" value="1" size="2">
<input name="shipping" type="radio" value="slow">

<input name="shipping" type="radio" value="fast">
<input name="total" type="text" id="total" size="8">
I 'd like that when someone checks the first radiobutton (slow),
the "total" textfield shows: price x quantity + 5

and when someone checks the second radiobutton (fast),
the "total" textfield shows: price x quantity + 10
Sadly, I don't know javascript, but there's got to be an easy way to do
that, right?

Thanks to all who will answer, any link to a page that has something
similar would also be appreciated.


Jul 20 '05 #2

P: n/a
Lee
elji said:
<input name="price" type="text" id="price" value="100" size="4">
<input name="quantity" type="text" id="quantity" value="1" size="2">
<input name="shipping" type="radio" value="slow">
<input name="shipping" type="radio" value="fast">
<input name="total" type="text" id="total" size="8">

I 'd like that when someone checks the first radiobutton (slow),
the "total" textfield shows: price x quantity + 5

and when someone checks the second radiobutton (fast),
the "total" textfield shows: price x quantity + 10
Sadly, I don't know javascript, but there's got to be an easy way to do
that, right?


Is this for a class?
It seems too simplistic to be for a real website, and I'd hate
to think that somebody who doesn't know Javascript would try
to create a commercial web site. You could open yourself to
all sorts of legal and financial problems.

Jul 20 '05 #3

P: n/a
Lee wrote:
elji said:

<input name="price" type="text" id="price" value="100" size="4">
<input name="quantity" type="text" id="quantity" value="1" size="2">
<input name="shipping" type="radio" value="slow">
<input name="shipping" type="radio" value="fast">
<input name="total" type="text" id="total" size="8">

I 'd like that when someone checks the first radiobutton (slow),
the "total" textfield shows: price x quantity + 5

and when someone checks the second radiobutton (fast),
the "total" textfield shows: price x quantity + 10
Sadly, I don't know javascript, but there's got to be an easy way to do
that, right?

Is this for a class?
It seems too simplistic to be for a real website, and I'd hate
to think that somebody who doesn't know Javascript would try
to create a commercial web site. You could open yourself to
all sorts of legal and financial problems.

No, that's not for a class, this looks simplistic, but that's only a
part of a very large form, but I've only shown here where my problem is.

And yes, I hardly know javascript, but my commerce is not about programming.
Jul 20 '05 #4

P: n/a
Lee wrote:
Is this for a class?


What do you mean by `class'?
PointedEars
Jul 20 '05 #5

P: n/a
elji wrote:
<input name="price" type="text" id="price" value="100" size="4">
<input name="quantity" type="text" id="quantity" value="1" size="2">
<input name="shipping" type="radio" value="slow">
<input name="shipping" type="radio" value="fast">
<input name="total" type="text" id="total" size="8">

I 'd like that when someone checks the first radiobutton (slow),
the "total" textfield shows: price x quantity + 5
function calcTotal(o)
{
if (o
&& o.value
&& o.form
&& o.form.elements
&& o.form.elements['price']
&& o.form.elements['quantity']
&& o.form.elements['total'])
{
var total =
o.form.elements['price'].value * o.form.elements['quantity'].value;
var adds = {slow: 5, fast: 10};
total += (adds[o.value] ? adds[o.value] : 0);
o.form.elements['total'].value = total;
}
}
....
<input name="shipping" type="radio" value="slow"
onclick="calcTotal(this)">
and when someone checks the second radiobutton (fast),
the "total" textfield shows: price x quantity + 10
<input name="shipping" type="radio" value="fast"
onclick="calcTotal(this)">

The calcTotal(...) function decides what to add
depending on the `value' attribute of the radio
button. Untested.
Sadly, I don't know javascript,
But you can learn it.
but there's got to be an easy way to do that, right?


Yes, it is.
HTH

PointedEars
Jul 20 '05 #6

P: n/a
Thomas 'PointedEars' Lahn hu kiteb:
Lee wrote:
Is this for a class?


What do you mean by `class'?


Judging from context, I'd guess class as in a course of study.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.