问题描述
我正在使用 Drupal 7 字段 API 通过 AJAX 重新加载我的部分表单.我有一个可以拨打电话的按钮,但我想删除它并以编程方式拨打电话作为对特定事件的响应.这是我的 AJAX 按钮代码:
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',
),
);
(参见 http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#ajax 了解详情.)有没有办法做到这一点?
(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.
推荐答案
我认为有两种方法可以做到:
There are two ways you can do this, I think:
首先,您可能已经阅读过的 #ajax
属性接受 event
键.对于按钮元素,默认情况下,该事件为mousedown
.(您可以检查它这里)您可以将其更改为自定义事件,例如 customEvent
并从您的 Javascript 代码中触发此自定义事件,如 jQuery('#button-id').trigger('customEvent');
.
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');
.
或者,您可以使用 #ajax
属性本身.设置 AJAX 端点(使用 hook_menu
)并将 reload_document_list
设置为其回调.添加自定义 Javascript 以发出请求并处理响应.您可以查看使用 misc/ajax.js
中的 #ajax
时 Drupal 发送 AJAX 请求的方式,并在需要时将其用作参考.
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.
这篇关于Drupal 7 Field API:如何以编程方式发送表单元素的#ajax属性中指定的AJAX请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!