2013-08-23 30 views
0

我遇到了显示页面中元素的问题。根据角色的选择,我需要显示|隐藏选择元素。因此,即时通讯hidin`一个元素是这样的:显示/隐藏元素ajax Drupal

$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']; 
} 

感谢。

+0

ive已将#access属性更改为#disabled,因此它能够使用单选按钮打开/关闭组件,但我没有得到原因#access具有不同的行为.... – user2709715

回答

0
<code> 
$form['enter_amt'] = array(
    '#type' => 'checkbox', 
    '#title' => t('Enter Billable Amount'), 
); 
    $form['billable_ctc'] = array(
    '#type' => 'textfield', 
    '#title' => t('Billable CTC'), 
    '#maxlength' => '15', 
    '#field_prefix' => t('Rs.'), 
    '#states' => array('invisible' => array(':input[name="enter_amt"]' =>   array('checked' => TRUE),),), 
); 
    $form['billing_rate'] = array(
    '#type' => 'textfield', 
    '#title' => t('Billing rate'), 
    '#maxlength' => '7', 
    '#field_suffix' => '%', 
    '#size' => 3, 
    '#attributes' => array('class' => array('auto-width')), 
    '#states' => array('invisible' => array(':input[name="enter_amt"]' => array('checked' => TRUE),),), 
); 
</code> 

此代码适用于隐藏和显示基于单击复选框的特定表单元素。