在Drupal 7中,您可以使用$ form #states
代替自定义jQuery脚本。例:
$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'),
),
),
);
如果您要#states
用于多个值条件,请参见以下示例:
$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),
),
),
);
有关更多详细信息和示例,请参见“ form_example/form_example_states.inc
来自示例”模块。