我正在尝试使用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和名称,因此没用。
我正在使用方法“append”,因此我必须只为一行编写代码。我确定我做错了什么,但是在过去的4-6小时内无法解决这个问题。