In Drupal 7 puoi usare $ form #states
invece dello script jQuery personalizzato. Esempio:
$form['student_type'] = array(
'#type' => 'radios',
'#options' => array(
'high_school' => t('High School'),
'undergraduate' => t('Undergraduate'),
'graduate' => t('Graduate'),
),
'#title' => t('What type of student are you?')
);
// High school information.
$form['high_school']['tests_taken'] = array(
'#type' => 'checkboxes',
'#options' => drupal_map_assoc(array(t('SAT'), t('ACT'))),
'#title' => t('What standardized tests did you take?'),
// This #states rule says that this checkboxes array will be visible only
// when $form['student_type'] is set to t('High School').
// It uses the jQuery selector :input[name=student_type] to choose the
// element which triggers the behavior, and then defines the "High School"
// value as the one that triggers visibility.
'#states' => array(
'visible' => array( // action to take.
':input[name="student_type"]' => array('value' => 'high_school'),
),
),
);
Ecco un esempio se si desidera utilizzare #states
per la condizione di più valori:
$form['student_type'] = array(
'#type' => 'checkboxes',
'#options' => array(
'high_school' => t('High School'),
'undergraduate' => t('Undergraduate'),
'graduate' => t('Graduate'),
),
'#title' => t('What type of student are you?')
);
// High school information.
$form['high_school']['tests_taken'] = array(
'#type' => 'textfield',
'#title' => t('What standardized tests did you take?'),
'#states' => array(
'visible' => array( // action to take.
':input[name="student_type[high_school]"]' => array('checked' => TRUE),
':input[name="student_type[undergraduate]"]' => array('checked' => TRUE),
':input[name="student_type[graduate]"]' => array('checked' => FALSE),
),
),
);
Vedi il moduloform_example/form_example_states.inc
da esempi per maggiori dettagli ed esempi.