2013-08-02 43 views
2

如何在點擊Drupal中的鏈接時使用AJAX提交表單?drupal ajax通過點擊表單中的鏈接調用

function search_form($form,$form_state) { 
    $form['support_email'] = array(
    '#theme' => 'link', 
    '#text' => '', 
    '#ajax' =>array(
     'callback' => 'ajax_change', 
     'wrapper' => 'email-hidden', 
     'method' => 'replace', 
     'click' => 'true', 
    ), 
); 
} 

這是表格內的鏈接。點擊鏈接時,我想調用AJAX回調函數ajax_change,這似乎沒有發生。

+0

在關鍵'#text'中填寫一些文本,看它是否有效。 –

+0

沒有Vishal無法工作....我正在研究使用ctools到ajax現在提交表單。 – ssD

回答

1

#ajax功能的窗體API引用說,它是「使用:按鈕,複選框,複選框,圖像按鈕,密碼,收音機,收音機,選擇,提交,表格選擇,textarea,text_format,文本框。鏈接不在列表中,因此不起作用。 #ajax功能使Drupal在指定的表單元素更改時執行AJAX調用。由於鏈接不會改變,所以它不起作用是合乎邏輯的。

Asaf (ajax submit for any form)模塊可能能夠幫助您實現您想要做的事情。

此外,它似乎你正在嘗試使用此AJAX隱藏一個元素。 Forms API具有states功能,可以輕鬆有條件地顯示/隱藏元素。閱讀this瞭解更多有關州的信息。

#ajax回調函數用於生成一個動態改變自己的表單。

+0

我現在隱藏了表單上的提交按鈕,一旦我點擊鏈接提交表單。提交按鈕有一個#ajax,所以我通過它調用它.. – ssD

+0

你需要添加一些JavaScript/jQuery來做到這一點。您可以使用[「#attached」]屬性來包含JavaScript。爲什麼你希望用戶點擊一個鏈接而不是一個按鈕。一個表單可以有很多提交按鈕,你可以將它們的主題看起來像鏈接。 – Thomas4019

1

在Drupal中,不可能(不使用第三方模塊)使用鏈接標記作爲AJAX觸發元素。另一種解決方案可能是創建一個帶AJAX功能的隱藏按鈕元素,並在該隱藏按鈕上進行鏈接觸發點擊事件。

這裏是形式功能:

function mymodule__form($form, $form_state) { 
    // JS file contains the code for triggering click event on e hidden button. 
    $form['#attached']['js'][] = 
    drupal_get_path('module', 'mymodule') . '/js/mymodule.behaviors.js'; 

    // Our link element. 
    $form['link_mymodule'] = array(
    '#type' => 'link', 
    '#name' => 'link_mymodule', 
    '#title' => t('Perform mymodule logic'), 
    '#href' => current_path(), 
    '#options' => array(
     'attributes' => array(
     'class' => array('mymodule_ajax'), 
    ), 
    ), 
); 

    // Hidden AJAX enabled submit button. 
    $form['mymodule_ajax_submit'] = array(
    '#type' => 'button', 
    '#value' => 'AJAX submit', 
    '#name' => 'mymodule_ajax_submit', 

    // You may need this to disable validation. 
    '#limit_validation_errors' => array(), 

    '#ajax' => array(
     'callback' => '_mymodule__form__pager_callback', 
     'event' => 'click', 
    ), 
    '#attributes' => array(
     // Class "element-hidden" will hide the button. 
     'class' => array('element-hidden', 'mymodule_ajax_submit'), 
    ), 
); 

    // Some element for tests. 
    $form['random_thing'] = array(
    '#type' => 'markup', 
    '#markup' => rand(1, 10000), 

    // Wrapper is needed since AJAX will use it as a container for incoming data. 
    '#prefix' => '<div class="ajax_wrapper">', 
    '#suffix' => '</div>', 
); 

    return $form; 
} 

您還需要回調函數與更換新老數據,因爲你正在使用AJAX:

function _mymodule__form__pager_callback($form, &$form_state) { 
    return array(
    '#type' => 'ajax', 
    '#commands' => array(
     ajax_command_replace('.ajax_wrapper', trim(render($form['random_thing']))), 
    ), 
); 
} 

你也必須附上單擊事件到您的鏈接,這將觸發隱藏按鈕上的點擊事件。這就是存儲在/js/mymodule.behaviors.js文件中的內容。

(function ($, Drupal) { 
    Drupal.behaviors.mymodule = { 
    attach: function (context, settings) { 

     // Since behaviors will be executed every times AJAX is called, it's better to use $.once() method even if you 
     // are going to use "context" argument. That is needed to be sure that event is attached only once. 
     $('.mymodule_ajax', context).once('mymodule_ajax', function() { 

     // Bind click event to out link. 
     $(this).click(function (e) { 
      // Prevent browser to follow the link. 
      e.preventDefault(); 

      // Perform click triggering. 
      $('input.mymodule_ajax_submit').click(); 
     }); 

     }); 

    } 
    } 
}(jQuery, Drupal)); 
+0

@MamounBenghezal我已添加您請求的代碼。可惜的是,唯一一個很難找到和工作的解決方案現在有負面投票,可能有人會錯過它。 –

+0

感謝您的版權警告@ArtjomB。我用免費的代碼和解釋寫了一個新的擴展答案,所以沒有問題了。謝謝你的評論。 –