通过单击表单中的链接调用drupal ajax
在Drupal中单击链接时,如何使用AJAX提交表单通过单击表单中的链接调用drupal ajax,ajax,forms,drupal,hyperlink,Ajax,Forms,Drupal,Hyperlink,在Drupal中单击链接时,如何使用AJAX提交表单 function search_form($form,$form_state) { $form['support_email'] = array( '#theme' => 'link', '#text' => '', '#ajax' =>array( 'callback' => 'ajax_change', 'wrapper' => 'email-hidden'
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\u change
,但这似乎没有发生。该功能的forms api参考说明它“由以下人员使用:按钮、复选框、复选框、图像按钮、密码、收音机、收音机、选择、提交、表选择、文本区域、文本格式、文本字段”。链接不在列表中,因此不起作用。#ajax功能使Drupal在指定表单元素更改时执行ajax调用。既然一个链接没有改变,那么它不起作用是合乎逻辑的
该模块可能会帮助您实现您想要做的事情
此外,您似乎正在尝试使用此AJAX隐藏元素。Forms API具有一项功能,可以方便地有条件地显示/隐藏元素。阅读了解更多关于州的信息
#ajax回调用于生成一个动态更改自身的表单 在Drupal中(不使用第三方模块)不可能使用link标记作为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;
}
此外,您还必须将单击事件附加到链接,这将触发隐藏按钮上的单击事件。这就是存储在/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));
在“#text”键中填写一些文本,看看它是否有效。No Vishal不起作用。。。。我正在研究使用ctools来ajax提交表单。我现在隐藏表单上的提交按钮,一旦我点击了提交表单的链接。Submit按钮有一个#ajax,所以我通过它调用它。您需要添加一些javascript/jQuery来实现这一点。您可以使用[“#attached”]属性包含JavaScript。为什么希望用户单击链接而不是按钮。一个表单可以有许多提交按钮,您可以将它们设置为链接的主题。@Mamounbenghzal我已经添加了您请求的代码。遗憾的是,目前唯一一个难以找到且有效的解决方案获得了反对票,可能有人会错过它。感谢您的版权警告,@ArtjomB。我已经写了一个新的扩展答案,里面有免费的代码和解释,所以没有问题了。谢谢你的评论。
(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));