我在使用 FAPI 创建的表单的提交按钮中使用 #ajax。现在,当用户提交表单时,我想在通过ajax提交表单之前运行一些jQuery验证。AS #ajax 防止与提交按钮相关的事件,例如提交,单击,鼠标按下,按键等。我无法使用jQuery捕获提交事件。
现在作为解决方法,我在 ajax.js (misc/ajax.js) 中添加了自定义代码:
Drupal.ajax = function (base, element, element_settings) {
...
beforeSubmit: function (form_values, element_settings, options) {
//my custom code
alert(1);
...
这违反了drupal最佳实践,因为我正在破解核心。请任何人都可以帮助我从我的自定义 js 文件或任何其他方法中执行相同的操作,以便在 ajax 提交之前验证内容。
我认为以下帖子中接受的答案回答了您的问题:如何扩展或"钩住"Drupal Form AJAX?
(function($) {
Drupal.behaviors.MyModule = {
attach: function (context, settings) {
// Overwrite beforeSubmit
Drupal.ajax['some_element'].options.beforeSubmit = function (form_values, element, options) {
// ... Some staff added to form_values
}
//Or you can overwrite beforeSubmit
Drupal.ajax['some_element'].options.beforeSerialize = function (element, options) {
// ... Some staff added to options.data
// Also call parent function
Drupal.ajax.prototype.beforeSerialize(element, options);
}
//...
我把它放在附加函数中
for (var base in settings.ajax) {
Drupal.ajax[base].options.beforeSubmit = function(form_values, element, options){
console.log('call your func');
}
}
它有效!