Drupal的7现场的API:如何以编程方式发送的表单元素#ajax属性指定的AJAX请求?表单、属性、元素、现场

2023-09-10 20:00:21 作者:对什么人给什么脸色

我使用Drupal的7场API重新加载通过AJAX我的一部分。我有一个按钮,这使得呼叫,但我想删除它,然后拨打电话编程为一个特定事件的响应。这是我的code为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。组织/ API / Drupal的/开发者!主题!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。我知道我可以风格的按钮使不可见并触发单击事件,但我正在寻找一个更合适的方法来做到这一点。

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 属性你可能已经阅读接受一个事件键。对于默认按钮元素,本次活动是鼠标按下。 (您可以检查here)您可以将其更改为一个自定义事件,说自定义事件键,触发你的Javascript code这个自定义事件的的jQuery('#键-ID ').trigger(自定义事件');

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发出请求和处理响应。你可以看看Drupal的发送当您使用AJAX请求的方式 #ajax 杂项/ ajax.js 并使用它作为一个参考,如果你想要的。

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.