Jquery: Conditional Show An Element Based On Drop Down Box Selection
Solution 1:
I think iKnowKungFoo's answer is very straightforward (it's got my vote). I noticed you said your form is generated by Django though. In case it's not straightforward for you to modify your generated HTML markup, here is another solution to your problem.
$(document).ready(function() {
var$aerialTr = $('#id_A').closest('tr').hide();
var$groundSprayTr = $('#id_B').closest('tr').hide();
$('#id_application_method').change(function() {
var selectedValue = $(this).val();
if(selectedValue === 'A') {
$aerialTr.show();
$groundSprayTr.hide();
} elseif (selectedValue === 'B') {
$aerialTr.hide();
$groundSprayTr.show();
} else {
$aerialTr.hide();
$groundSprayTr.hide();
}
});
});
Here is a jsFiddle to test: http://jsfiddle.net/willslab/n54cE/2/
It should work with your existing markup. It selects the tr's based on the current IDs for the select boxes. If you change those IDs, you will need to modify the selectors accordingly.
I hope that helps!
Edit: Here is another alternative, "hybrid" approach inspired by iKnowKungFoo. His solution is very elegant, so I combined it with my own. This works without changes to HTML or CSS.
$(document).ready(function() {
$('#id_A').closest('tr').addClass('method_options').hide();
$('#id_B').closest('tr').addClass('method_options').hide();
$('#id_application_method').change(function() {
$('tr.method_options').hide();
$('#id_' + $(this).val()).closest('tr').show();
});
});
jsFiddle link: http://jsfiddle.net/willslab/6ASJu/3/
Solution 2:
Your questions describe the right ideas. You just have to structure your HTML to take advantage of them.
JSFiddle posted here: http://jsfiddle.net/iknowkungfoo/TKamw/
HTML - I added an ID and CLASS to each TR that match the values in your primary SELECT:
<divclass="articles"><formmethod="get"action="_output.html"><tablealign="center"><tr><th><labelfor="id_application_method">Application method:</label></th><td><selectname="application_method"id="id_application_method"><optionvalue="">Pick first</option><optionvalue="A">Aerial</option><optionvalue="B">Ground</option></select></td></tr><trid="tr_A"class="method_options"><th><labelfor="id_A">Aerial Size Dist:</label></th><td><selectname="aerial_size_dist"id="id_A"><optionvalue="A1"selected="selected">A1</option><optionvalue="A2">A2</option></select></td></tr><trid="tr_B"class="method_options"><th><labelfor="id_B">Ground spray type:</label></th><td><selectname="ground_spray_type"id="id_B"><optionvalue="B1"selected="selected">B1</option><optionvalue="B2">B2</option></select></td></tr></table></form></div>
CSS - hide those TRs by default:
tr.method_options { display: none; }
JavaScript/jQuery - When the primary SELECT changes, hide all TRs with a CLASS of "method_options". Then, Find the TR whose ID matches the value of the selected option in the primary SELECT and show it. If there is no match, then nothing is shown.
$(document).ready(function(){
$('#id_application_method').on('change', function() {
$('tr.method_options').hide();
$('#tr_' + $(this).val() ).show();
});
});
Post a Comment for "Jquery: Conditional Show An Element Based On Drop Down Box Selection"