Forum Moderators: open
<!DOCTYPE html>
<html>
<head>
<title>Multiple Dropdowns Change Values</title>
</head>
<body>
<form>
<div>
<select name="product">
<option value="1.23">Product 1</option>
<option value="2.34">Product 2</option>
<option value="3.45">Product 3</option>
</select>
<select name="qty">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" id="buyBtn" value="Buy">
</div>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
/* Some very basic CSS. YMMV. */
#total {
font-size: 200%;
font-weight: bold;
}
a#buyBtn {
padding: 1em;
border: 1px solid #000;
color: #fff;
text-decoration: none;
background: #e36c04;
}
</style>
<title>Multiple Dropdowns Change Values</title>
</head>
<body>
<form>
<div>
<select name="product">
<option value="1.23">Product 1</option>
<option value="2.34">Product 2</option>
<option value="3.45">Product 3</option>
</select>
<select name="qty">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" id="buyBtn" value="Buy">
</div>
</form>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// Replace the submit button with a place to display the total and a
// stylized link 'button'. Don't forget to add CSS for #total and a#buyBtn.
$('#buyBtn').replaceWith("<span id='total'><\/span><a id='buyBtn' href='#'>Buy<\/a>");
$('#buyBtn').click(function (e) {
e.preventDefault();
$('form').submit();
});
function updateTotal() {
// TODO: add some Math functions to ensure decimal precision
$('#total').html('$' + ($('select[name=product]').val() * $('select[name=qty]').val()));
// This is just to provide a nicer hover experience since our Buy button
// is simply going to trigger the form submit.
$('#buyBtn').get()[0].href = ($('form').attr('action') || '?') + $('form').serialize();
}
// Update the total immediately (using the default select box values)
updateTotal();
// Attach listeners to the select boxes. I could have just defined 1
// listener here using $('select'), since the only select elements in the
// HTML above are the ones I want. But this is more precise.
$('select[name=product]').change(updateTotal);
$('select[name=qty]').change(updateTotal);
});
</script>
</body>
</html>
I came across some javaScript and wonder if I can go without it.
<form action="dd-html.php" method="get">
<select id="widget" name="widget">
<option value="wid1" selected="wid1">1 Widget</option>
<option value="wid2">2 Widgets</option>
</select>
<select id="option" name="option">
<option value="opt1" selected="opt1">Option 1</option>
<option value="opt2">Option 2</option>
</select>
<input type="submit" value="Buy now" />
</form>
<script type="text/javascript">
function addit(){
if(document.getElementById("add").value=="1")
{
document.getElementById("amount").value="$5.00"
}
if(document.getElementById("add").value=="3")
{
document.getElementById("amount").value="$10.00"
}
if(document.getElementById("add").value=="6")
{
document.getElementById("amount").value="$15.00"
}
}
</script>
<body>
<form action="/accounts/cart.php" method="get">
<table class="comparison pricing-email" border="0" cellspacing="1" cellpadding="0" width="30%">
<tbody>
<tr class="row1">
<td class="col1">Choose # of EMAIL accounts:
<select style="width: 60px; font-size: 17px;" name="add" id="add" onChange="addit()">
<option value="1">1</option>
<option value="3">3</option>
<option value="6">6</option>
</select>
</td>
</tr>
<tr class="row1">
<td class="col1">Per Month: <input type="text" id="amount" value="" style="border:none; font-size:13pt;"></td>
</tr><tr>
<td>
<input type="submit" name="submit" value="Sign-Up">
</td>
</tr>
</tbody>
</table>
</form>
</body>
var priceMap = {
add1: "$5.00",
add3: "$10.00",
add6: "$15.00"
};
function addit(el){
var val = priceMap[el.id + el.value];
if (val) {
document.getElementById("amount").value = val;
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function addit(){
if(document.getElementById("widget").value=="wid1" && document.getElementById("option").value=="opt1")
{
document.getElementById("amount").value="$5.00"
}
if(document.getElementById("widget").value=="wid1" && document.getElementById("option").value=="opt2")
{
document.getElementById("amount").value="$10.00"
}
if(document.getElementById("widget").value=="wid2" && document.getElementById("option").value=="opt1")
{
document.getElementById("amount").value="$15.00"
}
if(document.getElementById("widget").value=="wid2" && document.getElementById("option").value=="opt2")
{
document.getElementById("amount").value="$20.00"
}
}
</script>
</head>
<body>
<form action="dd-html.php" method="get">
<select id="widget" name="widget" onChange="addit()">
<option value="wid1" selected="wid1">1 Widget</option>
<option value="wid2">2 Widgets</option>
</select>
<select id="option" name="option"onChange="addit()">
<option value="opt1" selected="opt1">Option 1</option>
<option value="opt2">Option 2</option>
</select>
<input type="submit" value="Buy now" />
<input type="text" id="amount" value="" style="border:none; font-size:13pt;">
</select>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var priceMap = {
wid1opt1: "$5.00",
wid1opt2: "$10.00",
wid2opt1: "$15.00",
wid2opt2: "$20.00"
};
function addit() {
var w = document.getElementById('widget'),
o = document.getElementById('option'),
val = priceMap[w.value + o.value];
if (val) {
document.getElementById("amount").value = val;
}
}
</script>
</head>
<body>
<form action="dd-html.php" method="get">
<select id="widget" name="widget" onChange="addit()">
<option value="wid1" selected="wid1">1 Widget</option>
<option value="wid2">2 Widgets</option>
</select>
<select id="option" name="option"onChange="addit()">
<option value="opt1" selected="opt1">Option 1</option>
<option value="opt2">Option 2</option>
</select>
<input type="submit" value="Buy now" />
<input type="text" id="amount" value="" style="border:none; font-size:13pt;">
</form>
</body>
</html>
[edited by: tedster at 12:53 am (utc) on Dec 7, 2010]
[edit reason] added a line break to the DTD - stop side scroll [/edit]