Drupal 7 Field API: how to programmatically send AJAX request specified in #ajax property of form element?

醉酒当歌 提交于 2019-12-24 03:35:12

问题


I'm using Drupal 7 field API to reload part of my form through AJAX. I have a button which makes the call but I would like to remove it and make the call programmatically as a response to a specific event. Here is my code for the AJAX button:

$form['documents']['reload_document_list_button'] = array(
  '#type' => 'button',
  '#value' => 'Обновить список документов',
  '#ajax' => array(
    'callback' => 'reload_document_list',
    'wrapper' => 'document-list',
    'method' => 'replace',
  ),
);

(See http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#ajax for details.) Is there a way to do this?

P.S. I know I can style the button to make it invisible and trigger 'click' event, but I am looking for a neater way to do this.


回答1:


There are two ways you can do this, I think:

First, the #ajax property as you might have read accepts an event key. For the button element by default, this event is mousedown. (You can check it here) You can change it to a custom event, say customEvent and trigger this custom event from your Javascript code as jQuery('#button-id').trigger('customEvent');.

Alternatively, you can chuck the #ajax property itself. Set up an AJAX endpoint (using hook_menu) and set reload_document_list as its callback. Add custom Javascript to make the request and handle the response. You can take a look at the way Drupal sends the AJAX request when you use #ajax from misc/ajax.js and use that as a reference if you want.



来源:https://stackoverflow.com/questions/11039088/drupal-7-field-api-how-to-programmatically-send-ajax-request-specified-in-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!