PDA

View Full Version : Forcing a value into a text box from a combo selection



Frogger
04-22-2006, 03:08 AM
Hi,

I am trying to force a value in a text box from selecting a combo box but for some reason I cant get the IF statement working.
I want to be able to select a value from the combo box and when doing so it will put a number value in the text box.


<script Language="JavaScript">
<!--

function compute(form)
{

if (baskets=="toast")
{
document.basketorder.price.value = 40;
}

}

//-->
</script>
<body>

<FORM METHOD="POST" ACTION="http://mailgate.server-mail.com/cgi-bin/mailgate" name="basketorder">

<select name="baskets" onClick="compute(this.form)">
<option value="">- Select -</option>
<option value="toast">A Toast</option>
<option value="simple">Simple Pleasures</option>
<option value="fine">Fine Fare</option>
<option value="party">Party Time</option>
</select>

<input name="price" type="text">
</form>

Thanks for your help with this.

Frogger

Twey
04-22-2006, 01:15 PM
<script type="text/javascript">
var prices = new Object();
prices.toast = 40;
prices.simple = 50;
prices.fine = 60.5;
prices.party = 112.34;
</script>

<body>
<form method="post" action="http://mailgate.server-mail.com/cgi-bin/mailgate" name="basketorder">
<select name="baskets" onchange="this.form.elements['price'].value = (typeof prices[this.value] != 'undefined' ? prices[this.value] : '');">
<option value="" disabled="true" selected="true">- Select -</option>
<option value="toast">A Toast</option>
<option value="simple">Simple Pleasures</option>
<option value="fine">Fine Fare</option>
<option value="party">Party Time</option>
</select>
<input name="price" type="text" readonly="true">
</form>

Frogger
04-23-2006, 09:23 PM
Thanks Twey,

Just wondering if I could ask for your help one more time.
I have added 2 more fields but I want to be able to make the final total value change when either the - select - or -qty - combo boxes are changed.
Meaning that total input box will show a new value anything time the 2 combo boxes change.

Thanks
Frogger

<script type="text/javascript">
var prices = new Object();
prices.toast = 40;
prices.simple = 50;
prices.fine = 60.5;
prices.party = 112.34;


function compute(form)
{

total = (eval(form.price.value * form.qty.value));

document.basketorder.total.value = total;


}


</script>



<body>
<form method="post" action="http://mailgate.server-mail.com/cgi-bin/mailgate" name="basketorder">

<select name="baskets" onchange="this.form.elements['price'].value = (typeof prices[this.value] != 'undefined' ? prices[this.value] : '');">
<option value="" disabled="true" selected="true">- Select -</option>
<option value="toast">A Toast</option>
<option value="simple">Simple Pleasures</option>
<option value="fine">Fine Fare</option>
<option value="party">Party Time</option>
</select>

<input name="price" type="text" readonly="true" onFocus="this.blur()">

<select name="qty" onchange="compute(this.form)">
<option value="" disabled="true" selected="true">- Qty -</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>

</select>

<input name="total" type="text" onFocus="this.blur()">

<input type="submit" name="Submit" value="Submit">
<input type="reset" name="reset" value="Reset">

</form>

Twey
04-24-2006, 05:39 AM
<select name="baskets" onchange="this.form.elements['price'].value = ((typeof prices[this.value] != 'undefined' ? prices[this.value] : '') * (isNaN(parseInt(this.form.elements['qty'].value)) ? 1 : parseInt(this.form.elements['qty'].value)));">And try:
<input name="total" type="text" onfocus="this.blur();" readonly="true">

Frogger
04-24-2006, 07:05 AM
Thanks for your reply.
Sorry I dont think I made it clear to what I want.
Basically any time you change the baskets and the qty I want to make the total field re-calculate. The price field will will only correspond to the price of the baskets. ie can only be:

prices.toast = 40;
prices.simple = 50;
prices.fine = 60.5;
prices.party = 112.34

thanks
Jason

Twey
04-24-2006, 02:54 PM
Oh, I'm sorry. Try:
<select name="baskets" onchange="this.form.elements['price'].value = (typeof prices[this.form.elements['baskets'].value] != 'undefined' ? prices[this.value] : '');this.form.elements['total'].value = ((typeof prices[this.value] != 'undefined' ? prices[this.value] : '') * (isNaN(parseInt(this.form.elements['qty'].value)) ? 1 : parseInt(this.form.elements['qty'].value)));">

Frogger
04-25-2006, 03:14 AM
Thats great. Thanks for your help with this.

Frogger