我正在使用Drupal 7表单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['#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();
}
});