javascript - Drupal 7 #ajax 更改事件阻止同一元素上的其他更改事件处理程序

标签 javascript jquery drupal drupal-7

我正在使用 Drupal 7 Form Api 构建带有选择元素的自定义表单。我将 #ajax 回调附加到它,它将在更改事件时触发。

$form['landing']['country'] = array(
    '#type' => 'select',
    '#options' => array(),
    '#attributes' => array('class' => array('landing-country-list')),
    '#validated' => TRUE,
    '#prefix' => '<div id="landing-countries" class="hide">',
    '#suffix' => '</div>',    
    '#title' => 'Select country',
    '#ajax' => array(

      'wrapper' => 'landing-cities',

      'callback' => 'get_cities',

      'event' => 'change',

      'effect' => 'none',

      'method' => 'replace'

    ),    
);

但问题是它阻止了 js 中同一选择的自定义更改功能。在此函数中,我想获取选定的选项值。所以这不会触发:

$('body').on('change', 'select.landing-country-list', function() {
    optval = $(this).find('option:selected').val();
});

此代码位于文件中,我将其包含在 $form 中:

$form['#attached']['js'] = array(
    'https://code.jquery.com/jquery-2.2.4.min.js',
    drupal_get_path('module', 'landing') . '/landing.js',
);

预先感谢您的帮助!

最佳答案

如果你想在ajax发送之前捕获你可以使用:

$(document).ajaxSend(function(){
    var val = $('select.landing-country-list').val();
});

否则,如果你想在 ajaxcallback 之后获取值:

    $(document).ajaxComplete(function(event, xhr , options) {
          if(typeof options.extraData != 'undefined' && options.extraData['_triggering_element_name'] === 'country'){
// only on ajax event attached to country select
               var val =  $('select.landing-country-list').val();
          }
    });

关于javascript - Drupal 7 #ajax 更改事件阻止同一元素上的其他更改事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42553450/

相关文章:

javascript - 递归的定义是什么

javascript - JSON 文件已正确获取,但获取的数据未显示在控制台日志中

javascript - 为什么在 2 个检索值相加后无法执行 toFixed() 函数?

sql - 使用 MySQL 从 Ubercart 表中提取 Drupal 编码数据

javascript - 我的混合应用程序在检查包含字符串的 li 元素时不会更改页面

javascript - 三.javascript中的JS + OOP,无法将3D JSON对象传递给其他类

javascript - 如何引用 jQuery .append() 添加的元素

javascript - jQuery Accordion/Tabs - 在加载内容时调用不同的函数

javascript - 在 Drupal 8 中附加或包含一个 js 文件

Drupal 用户权限,只允许特定用户编辑特定页面