2017-03-02 34 views
0

我正在使用Drupal 7 Form Api构建带select元素的自定义窗体。我附加了#ajax回调函数,它将触发change事件。Drupal 7 #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['#attached']['js'] = array(
    'https://code.jquery.com/jquery-2.2.4.min.js', 
    drupal_get_path('module', 'landing') . '/landing.js', 
); 

预先感谢您的帮助!

回答

1

如果你想发送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(); 
      } 
    }); 
相关问题