Drupal 7 开发内部培训资料, 分头诗人

ajax提交后reset form的问题

赞成!
0
否决!
function og_blocks_say_words_form($form, &$form_state){
	$form = array();
	$form['#theme'] = 'og_block_say_words_form';
	$form['title'] = array(
		'#type' => 'textfield',
		'#theme_wrappers' => array(),
	);
// 	$form["wrapper"] = array("#markup" => "<div id='wrapper'></div>");
	$form['submit'] = array(
			'#type' => 'submit',
			'#value' => t('发布'),
			'#ajax' => array(
					'callback' => 'o g_blocks_say_words_submit',
					'wrapper' => 'o g-blocks-say-words-form',
					'method' => 'replace',
					'effect' => 'fade',
			),
	);
	return $form;
}

function og_blocks_say_words_submit($form, &$form_state){
	if (!empty($form_state['values']['title'])) {
		$new_form_state = array();
		$new_form_state['build_info']['args'] = array();
		$new_form_state['input'] = array();
		$new_form_state['values'] = array();
		$new_form_state['form_id'] = $form_state['form_id'];
		$new_form_state['method'] = $form_state['method'];
		$new_form = drupal_build_form('og_blocks_say_words_form', $new_form_state);
		return $new_form;
	}else{
		return $form;
	}
}

请大家看看上述代码,第一次提交时确实清除了表单的所有值,第二次提交就不行了,仔细检查html,原来form ID号变了,action也变了,各种奇怪!

代码还要改哪里呢?谢谢!

1 个回答

赞成!
0
否决!
<?php
  if (!empty($form_state['executed'])) {
    $form['name']['#value'] = '';
  }
?>

这样能对其中一个元素进行操作,如果你想对所有元素reset,那么你可以通过foreach循环:

<?php
  if (!empty($form_state['executed'])) {
    foreach ($form as $element) {
      if (is_array($element)) {
        if (isset($element['#value'])) {
          $form[$element]['#value'] = '';
        }
      }
    }
  }
  return $form;
?>

你尝试一下这样写