如何使用Drupal表单API创建后退按钮?

时间:2011-11-14 20:43:39

标签: forms api drupal button onclick

我需要这样做但是使用Drupal形式:

<input type="button" class="button-user" value="Back" onclick="location.href='edit'"/>

我尝试过这样做,但它不起作用:

$form['back']['#prefix'] = "<input type='button' class='button-user' value='Back' onclick='location.href='edit''/>;

还有:

$form['back'] = array(
  '#type' => 'button',
  '#value' => 'Back',
  '#attributes' => array(
     'class' => 'button-user',
     'onclick' => 'location.href=edit',        
   )       
 );

7 个答案:

答案 0 :(得分:4)

$form['back']['#markup'] = "<input type='button' class='button-user' value='Back' onclick='location.href=\'edit\''/>";

答案 1 :(得分:3)

$form['back']['#markup'] = "<input type='button' class='button-user' value='Back' onclick='window.history.go(-1)'/>";

这适用于任何页面。

答案 2 :(得分:1)

只需添加我的版本,它似乎在7中工作得很好,只需在重建周期中捕获它并重定向。可扩展,可以添加任何其他按钮来做事情,注意价值的拼写&#34;返回&#34;是&#34; op&#34;的名称。 (操作)......在我弄清楚之前,让我感到困惑和烦恼的事情。

function mymodule_something_form($form,&$form_state){

    //... Rest of form

    $form['unused_form_id_back'] = array(
        '#type' => 'button',
        '#value' => 'Back',
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Do Stuff!'
    );
    return $form;
}

function mymodule_something_form_validate($form, &$form_state)
{
    if($form_state['values']['op'] == 'Back'){
        drupal_goto('something/that_page');
    }
}

答案 3 :(得分:1)

Drupal Form API中最简单的选项,使用 #attributes 选项。

$form['back-btn'] = array(
    '#type'                 => 'button',
    '#value'                => t('Back'),
    '#attributes'           => array('onclick' => onclick='window.history.back();'),
);

答案 4 :(得分:0)

另一个解决方案就是这个:

function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) {
  switch($form_id) {
    case "YOUR_FORM_ID":

      unset($form['#validate']); //Maybe necessary

      $form['actions']['back'] = array(
        "#type" => "button",
        "#value" => t('Back'),
        "#weight" => 15,
        "#executes_submit_callback" => FALSE,
        '#limit_validation_errors' => array(),
        '#ajax' => array(
          'callback' => 'YOUR_MODULE_go_back_callback'
        )
      );

      break;
    default:
      break;
  }
}

function YOUR_MODULE_go_back_callback() {
  $html = '
  <script type"text/javascript">
  window.history.go(-1);
  </script>
  ';
  return $html;
}

答案 5 :(得分:0)

我更喜欢不需要JavaScript的解决方案。类似于Grizly的答案,但没有把它放在表格验证中,这感觉很难看。但是下面的代码提供了一个链接按钮。

function my_form(&$form, &$form_state) {
  // Some form elements

  // Regular submit button
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  // Back button (different submit handler prevent the standard submit and takes us
  // to the redirect-submit).
  $form['actions']['back'] = array(
    '#type' => 'submit',
    '#value' => t('Go back'),
    '#submit' => array('custom_back_button'),
  );
}

// Custom form callback for redirection.
function custom_back_button($form, &$form_state) {
  $form_state['redirect'] = '/edit';
}

答案 6 :(得分:0)

这是我使用的后退按钮。 “return false”避免表单提交。

 $form['back'] = array(
  '#type' => 'button',
  '#value' => t('<< Back'),
  '#attributes' => array(
    'onclick' => 'window.history.back();return false;',
  ),
);