我想在Drupal中创建一个表单,因此可以将多个元素添加到表单中。例如,一个页面可能包含一个事件的数据,那么该事件可能有多个日期。所以,我有,看起来像一个形式:Drupal AJAX回调只调用一次
/**
* Implements hook_form_alter().
*/
function addextra_form_alter(&$form, &$form_state) {
if ($form['#form_id'] == 'test_content_node_form') {
$form['elements_table'] = array(
'#theme' => 'table',
'#title' => 'Elements already added',
'#header' => array('Item', 'Remove'),
'#empty' => 'No elements',
'#prefix' => '<div id="elements-table">',
'#suffix' => '</div>',
);
$form['add_elements'] = array(
'#title' => 'Add another element',
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['add_elements']['add_content'] = array(
'#type' => 'textfield',
'#description' => t('Add an element to the table'),
'#title' => t('Add another item'),
'#size' => '12',
'#maxlength' => '60',
'#prefix' => '<div id="addextra_content">',
'#suffix' => '</div>',
);
$form['add_elements']['add_another_btn'] = array(
'#type' => 'button',
'#name' => 'add_another',
'#button_type' => 'submit',
'#executes_submit_callback' => FALSE,
'#value' => 'Add another',
'#ajax' => array(
'callback' => 'addextra_element_to_table',
),
);
}
}
当“add_another_btn”被点击,它会运行Ajax回调“addextra_element_to_table。
该回调是:
function addextra_element_to_table(&$form, &$form_state) {
$form['elements_table']['#rows'][] = array($form_state['values']['add_content'], l('Remove Item', '#'));
drupal_add_js(drupal_get_path('module', 'addextra') . '/addextra.js');
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace('#elements-table', render($form['elements_table'])),
),
);
}
叫JS文件替换输入字段为 ''
(function ($) {
$('#edit-add-content').val('');
})(jQuery);
的VAL但只有这个回调函数被调用一次。我相信这是因为一旦被调用,行为必须重新被附加。对不起,我的无知 - 我不知道如何实现这一点。谁能帮我吗?这将非常感激。提前致谢。
我不是Drupal的专家,但是你所做的是在已经加载的html页面中一次又一次添加相同的js文件,并期望每次调用'$(document).onload()'事件?看起来很奇怪。为什么使用Ajax来实现?难道你不能'克隆'一个(最终)隐藏的节点吗? – Stan 2011-12-28 02:50:05
我可以删除js,但替换为新元素的表单也不起作用。它不会在回调中添加'#row'数据的操作。 – Deshiknaves 2011-12-28 03:00:04
我已经移动了drupal_add_js,它按预期工作。每当我为add_content添加一个新值时,它都会运行并替换表。但它不添加到“#rows”,它只是用新值替换行,而不是加入一个新元素,则表更换表。任何想法将不胜感激。我知道还有其他的方法可以实现这一点,但我现在只想弄清楚如何以这种方式实现它,所以我可以在将来使用这种方法。 – Deshiknaves 2011-12-28 08:49:12