我在页面中显示元素时遇到问题。根据角色收音机的选择,我需要显示|隐藏选择元素。所以我隐藏了一个像这样的元素:
$form['field_test_field']['#access'] = 0;
但是当状态发生变化时,我无法通过绑定 1 将其恢复。这就是我的片段:
function seven_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['account']['roles']['#ajax'] = array(
'callback' => 'seven_dynamic_form_ajax_callback',
'wrapper' => 'replace_droplist',
'event' => 'change',
);
$form['field_test_field']['#prefix'] = '<div id="replace_droplist">';
$form['field_test_field']['#suffix'] = '</div>';
$form['account']['roles']['#type'] = 'radios';
$checkedRole = isset($form_state['values']['roles'])
? $form_state['values']['roles'] : 0;
if ($checkedRole == 0) {
$form['field_test_field']['#access'] = 1;
} elseif ($checkedRole == 4) {
$form['field_test_field']['#access'] = 0;
} elseif ($checkedRole == 3) {
$form['field_test_field']['#access'] = 1;
}
};
function seven_dynamic_form_ajax_callback($form, $form_state) {
return $form['field_test_field'];
}
谢谢。