2

我正在尝试使用 AHAH 在我的表单中添加一行表单元素。我的表单显示在表格中,因此我需要将表单元素添加为一行。

我将以下代码用于“添加更多”按钮:

$form['addmore'] = array(
  '#type' => 'button',
  '#value' => t('Add More'),
  '#ahah' => array(
    'path' => 'mymodule/add-more',
    'wrapper' => 'mytable-tbody', // mytable-tbody is the id of my form table tbody
    'method' => 'append',
    'effect' => 'fade',
  ),   
);

然后我为定义的路径定义一个菜单路径,其回调函数执行以下操作:

$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;

$form['customer-6-first_name'] = array(
  '#type' => 'textfield',
  '#title' => '',
  '#size' => '15',
  '#maxlength' => 128,
);
... ... (more such form elements)

$cells = array();
$cells[] = drupal_render($form['customer-6-first_name']);
... ... (more such form renders within the cells array)

$output = '<tr>';
foreach ($cells as $cell) {
  $output .= _theme_table_cell($cell);
}
$output .= " </tr>\n";

drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
drupal_json($output);

单击“添加更多”时,我确实得到了一个新行,并且还显示了表单元素,但是它们没有得到 id 和名称,因此没有用。

我正在使用“追加”方法,因此我必须只为一行编写代码。我确定我做错了什么,但在过去的 4-6 小时内无法弄清楚。

4

0 回答 0