在Drupal 7中提交AJAX表单之前如何显示确认框? - php

使用Drupal 7表单API,如何在提交AJAX表单之前提示javascript确认框?我尝试了多种可能的方法来完成此操作,但没有成功。这是代码:

function create_list_form($form, &$form_state) {

  $form['list_name'] = array(
    '#type' => 'textfield',
    '#required' => TRUE,
    '#title' => 'List Name'
    '#attributes' => array()
  );

  $form['list_desc'] = array(
    '#type' => 'textfield',
    '#required' => TRUE,
    '#title' => 'List Desc'
    '#attributes' => array()
  );

    $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#attributes' => array('class' => array('use-ajax-submit')),            
        '#value' => 'Create List'
      );


  return $form;
}

这是Javascript代码:

Drupal.behaviors.module = {
  attach: function() {

       jQuery('#edit-submit').click(function(){
          if(!confirm('Are you sure?'))
              return false;
      });

  }
}

参考方案

我找到了解决方案。我们可以通过重写beforeSerialize()函数来做到这一点:

 Drupal.behaviors.module = {
  attach: function() {  

     Drupal.ajax['edit-submit'].beforeSerialize = function () {  

          if(confirm('Are you sure?'))
              return true;
          else
              return false;

      }
   }
 }

45码