Drupal 7 Field API: how to programmatically send AJAX request specified in #ajax property of form element?(Drupal 7 Field API:如何以编程方式发送表单元素的#ajax属性中指定的AJAX请求?)
问题描述
我正在使用 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请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Drupal 7 Field API:如何以编程方式发送表单元素的#ajax属性中指定的AJAX请求?


- 如何显示带有换行符的文本标签? 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01