我一直在尝试在 drupal 7 中编写一个自定义模块,其中包含一组带有“添加更多”选项的字段,如“字段集合”(不想使用模块但编写控制代码)。我试图找到一个很好的例子,但没有成功。有人可以建议如何进行吗?请!!
提前致谢。
$form['education']['languages'] = array(
'#type' => 'fieldset',
'#title' => t('Languages'),
'#prefix' => '<div id="div_languages-fieldset-wrapper">',
'#suffix' => '</div>',
);
for ($i = 0; $i < $form_state['language_num']; $i++)
{
$form['education']['languages'][$i]['labels'] = array(
'#prefix' => '<h1 id = "stu_form_label">',
'#type' => 'item',
'#title' => t('Form :'.($i+1)),
'#suffix' => '</h1>',
);
// Language Drop down box
$option_language = _get_language_mstr();
$form['education']['languages'][$i]['language'] = array(
'#prefix' => '<div class="container-inline bottom_space languages_drop_down">',
'#options' => $option_language,
'#type' => 'select',
'#title' => t('Language'),
'#suffix' => '</div>',
);
// Level Drop down box
$option_Level = _get_level_mstr();
$form['education']['languages'][$i]['level'] = array(
'#prefix' => '<div class="container-inline bottom_space languages_drop_down">',
'#options' => $option_Level,
'#type' => 'select',
'#title' => t('Level'),
'#suffix' => '</div>',
);
}
if ($form_state['language_num'] > 1)
{
$form['education']['languages']['remove_name'] = array(
'#type' => 'submit',
'#value' => t('Remove last language'),
'#limit_validation_errors' => array(),
'#submit' => array('remove_one_language'),
'#ajax' => array(
'callback' => 'add_more_callback_language',
'wrapper' => 'div_languages-fieldset-wrapper',
),
);
}
$form['education']['languages']['add_name'] = array(
'#type' => 'submit',
'#value' => t('Add language'),
'#limit_validation_errors' => array(),
'#submit' => array('add_one_language'),
'#ajax' => array(
'callback' => 'add_more_callback_language',
'wrapper' => 'div_languages-fieldset-wrapper',
),
);
function add_more_callback_language($form, $form_state) {
return $form['education']['languages'];
}`
试试这个方法
示例链接:网址
点击标签教育