我有一个只包含一个文本字段的表单,预期的行为是当用户在文本框中按Enter键时,应该提交文本框的值,但不会重新页面(刷新)加载。 我已经尝试了各种方法来设置AHAH但仍然无法实现预期的行为,页面重新加载。 我没有运气就搜索了这个问题的任何解决方案。
请告知如何完成或指导我有用的地方 这是代码:
function user_porfile_message_form(){
$form = array();
$form['element1'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => '',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#attributes' => array('class' => 'no-js'),
'#ahah' => array(
'path' => 'module/file/callback',
'wrapper' => 'some-wrapper',
'event' => 'click',
),
);
return $form;
}
答案 0 :(得分:0)
得到解决方案,现在是:
function some_form(&$form_state){
$form = array();
$form['text'] = array(
'#value' => "Text",
'#prefix' => '<div id="some-wrapper">',
'#suffix' => '</div>',
);
$form['text_input'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => '',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#attributes' => array('class' => 'no-js'),
'#ahah' => array(
'path' => 'module/file/callback',
'wrapper' => 'some-wrapper',
'event' => 'click',
),
);
return $form;
}
function some_form_submit(){
//Do whatever has to be done for submission
}
function some_form_callback(){
$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;
drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
$changed_elements = $form['text'];
unset($changed_elements['#prefix'], $changed_elements['#suffix']);
drupal_json(array(
'status' => TRUE,
'data' => drupal_render($changed_elements),));
}
/**
* Implement hook_menu().
*/
function module_menu(){
$items['module/file/callback'] = array(
'page callback' => 'some_form_callback',
'access callback' => TRUE,
'file' => 'module.module',
'type' => MENU_CALLBACK,
);
return $items;
}