Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  39] [ 7]  / answers: 1 / hits: 25017  / 12 Years ago, tue, april 17, 2012, 12:00:00

I have two related drop-down lists, in which the contents in the second drop-down list depends on the selection made in the first one. For example, in the following HTML code, you will choose application method first. If you choose Aerial as the application method, then you will answer further question such as aerial size dist. Otherwise, you need to answer ground spray type.



So once the webpage is loaded, the two second level drop-down lists (aerial size dist., and ground spray type.) are hidden. They will appear only when related choice is made in the first one (application method).



I am able to achieve this feature in jQuery (below jQuery code). But my approach is pretty stupid. My question is:




  1. Is there a way to select the whole row, without using counting its sequence (nth-child())? Can I choose the whole row, based on selecting an element ID ? For example, can I first select $('#id_A') and then expand my selection to the whole row?


  2. Is there a better way (a loop?) to achieve this hide or show feature rather than comparing all the possible choices (($(this).val() == X) )?




Thanks!



Here is the HTML code, and the form is generated by Django:



<div class=articles>
<form method=GET action=_output.html>
<table align=center>
<tr><th><label for=id_application_method>Application method:</label></th><td><select name=application_method id=id_application_method>
<option value=>Pick first</option>
<option value=A>Aerial</option>
<option value=B>Ground</option>
</select></td></tr>

<tr><th><label for=id_A>Aerial Size Dist:</label></th><td><select name=aerial_size_dist id=id_A>
<option value=A1 selected=selected>A1</option>
<option value=A2>A2</option>
</select></td></tr>

<tr><th><label for=id_B>Ground spray type:</label></th><td><select name=ground_spray_type id=id_B>
<option value=B1 selected=selected>B1</option>
<option value=B2>B2</option>
</select></td></tr>
</table>
</form>
</div>


Here is the jQuery code:



<script type=text/javascript src= https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js></script> 

<script>$(function() {
$(tr:nth-child(2)).hide();
$(tr:nth-child(3)).hide();

$('#id_application_method').change(function() {
($(this).val() == A) ?
$(tr:nth-child(2)).show() : $(tr:nth-child(2)).hide();

($(this).val() == B) ?
$(tr:nth-child(3)).show() : $(tr:nth-child(3)).hide();
});});</script>

More From » jquery

 Answers
11

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();
} else if (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/


[#86178] Monday, April 16, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
allans

Total Points: 336
Total Questions: 120
Total Answers: 119

Location: Peru
Member since Mon, Jun 6, 2022
2 Years ago
;