我尝试使用带有4个元素(名称,服务器,版本,操作系统,拱门)的drupal 7创建表单
function create_server(){
$form['new_server']['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#required' => TRUE,
'#description' => t('Please make sure that your server\'s name is unique.')
);
$form['new_server']['os'] = array(
'#type' => 'select',
'#title' => t('OS'),
'#options' => $os,
'#multiple' => FALSE,
'#required' => TRUE,
'#prefix' => '<div id="os-div">',
'#suffix' => '</div>',
'#ajax' => array(
'callback' => 'ajax_server_version_callback',
'wrapper' => 'version-div',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['new_server']['version'] = array(
'#type' => 'select',
'#title' => t('Version'),
'#disabled' => TRUE,
'#options' => array(),
'#ajax' => array(
'callback' => 'ajax_server_arch_callback',
'wrapper' => 'arch-div',
'method' => 'replace',
'effect' => 'fade',
),
'#required' => TRUE,
'#prefix' => '<div id="version-div">',
'#suffix' => '</div>',
);
$form['new_server']['os_arch'] = array(
'#type' => 'select',
// '#disabled' => TRUE,
'#title' => t('OS_ARCH'),
'#options' => array(
'x86' => 'x86',
'x86_64' => 'x86_64'
),
'#multiple' => FALSE,
'#required' => TRUE,
'#prefix' => '<div id="arch-div">',
'#suffix' => '</div>',
);
return $form;
}
/**
* Ajax call for version select form
* based on the selection.
*/
function ajax_server_version_callback($form, &$form_state) {
$os = !empty($form_state['values']['os']) ? $form_state['values']['os'] : 'all';
$catalog = server_get_otib_content();
if(isset($catalog['version'][$os])){
$version = $catalog['version'][$os];
} else {
$version = $catalog['version'];
}
$form['version'] = array(
'#type' => 'select',
'#id' => 'edit-version',
'#title' => t('Version'),
'#disabled' => TRUE,
'#options' => $version,
'#ajax' => array(
'callback' => 'ajax_server_arch_callback',
'wrapper' => 'arch-div',
'method' => 'replace',
'effect' => 'fade',
),
'#required' => TRUE,
'#prefix' => '<div id="version-div">',
'#suffix' => '</div>',
);
/* echo '<pre>';
var_dump($form['version']);
echo '</pre>'; */
return $form['version'];
}
/**
* Ajax call for arch select form
* @param array $form
* @param array $form_state
* @return array
*/
function ajax_server_arch_callback($form, &$form_state) {
$os = !empty($form_state['values']['os']) ? $form_state['values']['os'] : 'all';
$version = !empty($form_state['values']['version']) ? $form_state['values']['version'] : 'all';
$catalog = server_get_otib_content();
if(isset($catalog['arch'][$os][$version])){
$version = $catalog['arch'][$os][$version];
} else {
$version = $catalog['arch'];
}
$form['new_server']['os_arch'] = array(
'#type' => 'select',
'#disabled' => TRUE,
'#title' => t('OS_ARCH'),
'#options' => array(
'x86' => 'x86',
'x86_64' => 'x86_64'
),
'#multiple' => FALSE,
'#required' => TRUE,
'#prefix' => '<div id="arch-div">',
'#suffix' => '</div>',
);
return $form['new_server']['os_arch'];
}
问题是当我选择第一个元素服务器时,它正确地进行了ajax回调并设置了版本元素字段。但即使设置了#ajax数组,version元素也不会设置ajaxcall来设置arch选择框。当我查看json返回ajax_server_version_callback时,它不包含ajax选项。 如果有人知道为什么它会非常有帮助。
THX
答案 0 :(得分:0)
我有同样的问题,无法谷歌解决方案。我意识到的一件事是,如果元素被ajax替换并且具有“禁用”属性,则ajax回调函数不起作用。 所以你可以尝试这样的事情:
改为添加some.js文件,该文件通过js代码
禁用元素(function($) {
$(document).ready(function(){
$('#element').attr('disabled', 'disabled');
});
})(jQuery);
它对我有用。