我有由 Profile2 创建的名为 field_apply 的复选框。然后我使用 hook_form_alter 添加一个 texfield,选中复选框时应该可见。
function test_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'user_register_form') {
$form['profile_test']['test'] = array(
'#type' => 'textfield',
'#title' => 'Test text',
'#states' => array(
'visible' => array(
':input[name="field_apply"]' => array('checked' => TRUE),
),
),
);
}
}
但 #states 不起作用。即使未选中field_apply文本字段仍可见。
我解决了它:)#state 中的字段名称错误。只需使用Firebug,选中field_apply复选框,复制"name"属性的值,并将"field_apply"替换为":input[name="field_apply"]"...因此,在上面的示例中 #states 属性应如下所示:
'visible' => array(':input[name="profile_test[field_apply][und]"]' => array('checked' => TRUE)),