dongyuan7981 2015-11-13 16:12
浏览 31
已采纳

Drupal AJAX没有更改下拉列表

I am working on code with a dependent dropdown list using the Form API for Drupal 7. The ajax request looks like its working (returning status 200) and I have know my options method is returning the correct array but it is still not changing the list... this is my code

$m1 = drupal_map_assoc(
        array(
                7,
            )
    );
$selected;
if(isset($form_state['values']['program'])){
    $m2 = drupal_map_assoc(
        array(
            (int)$form_state['values']['program'],
        )
    );
    $selected = key($m2); 
}else{
    $selected = key($m1);
}
$form['infocollect']['program'] = array(

        '#type' => 'select',
        '#title' => t('Program'),
        '#required' => TRUE,
        '#options' => array(
            0 => t('Option 1'),
            1 => t('Option 2'),
            2 => t('Option 3'),
            3 => t('Option 4'),
            4 => t('Option 5'),
            5 => t('Option 6'),
            6 => t('Option 7'),
        ),
        '#ajax' => array(
                'callback' => 'deposit_campus_dropdown_callback',
                'wrapper' => 'dropdown-campus-replace',
                'method' =>'replaceWith',
                ),
        '#prefix' => '<tr><td>',
        '#suffix' => '</td></tr>',

);
    $form['infocollect']['campus'] = array(
        '#type' => 'select',
        '#title' => t('Campus'),
        '#required' => TRUE,
        '#prefix' => '<tr><td><div id="dropdown-campus-replace">',
        '#suffix' => '</div></td></tr></table><br />',
        '#options' => _deposit_get_campus_options($selected),
);

......

function deposit_campus_dropdown_callback($form, $form_state) {
  return $form['campus'];
}

function _deposit_get_campus_options($key='') {
   dpm($key);

   $options = array(
    0 => drupal_map_assoc(
    array(
      t('Option 1'),
    )
    ),
    1 => drupal_map_assoc(
    array(
      t('Option 1'),
    )
    ),
    6 => drupal_map_assoc(
    array(
      t('Option 1'),
    )
    ),
    4 => drupal_map_assoc(
    array(
      t('Option 1'),
      t('Option 2'),
      t('Option 3'),
      t('Option 4'),
    )
    ),
    5 => drupal_map_assoc(
    array(
      t('Option 1'),
      t('Option 2'),
      t('Option 3'),
      t('Option 4'),
    )
    ),    
    3 => drupal_map_assoc(
    array(
      t('Option 1'),
      t('Option 2'),
      t('Option 3'),
    )
    ),
    2 => drupal_map_assoc(
    array(
      t('Option 1'),
      t('Option 2'),
      t('Option 3'),
    )
    ),
    7 => drupal_map_assoc(
    array(
      t('none'),
    )
    ),
  );
  if (isset($options[$key])) {
    dpm($options[$key]);
    return $options[$key];
  }
  else {
    return array();
  }
}

UPDATE: I have figured out that the problem is in the callback function deposit_campus_dropdown_callback , It is not finding $form['campus'] or $form['infocollect']['campus'] which is causing the ajax not to work, is there any way to get this fixed?

  • 写回答

3条回答 默认 最新

  • drsxobip501258 2015-11-16 18:04
    关注

    I have found an answer to this, I modified my callback function to be this

      $form['infocollect']['campus'] = array(
                '#type' => 'select',
                '#title' => t('Campus'),
                '#name' => 'campus',
                '#required' => TRUE,
                '#prefix' => '<div id="dropdown-campus-replace">',
                '#suffix' => '</div>',
                '#options' => _deposit_get_campus_options($form_state['values']['program']),
        );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 帮我做下照片上的PLC题
  • ¥15 labview2022 使用modbus报缺少依赖?
  • ¥15 谷歌地图是不是不开通结算功能,api会使用不了哦
  • ¥15 unity腾讯云对象存储机型适配
  • ¥15 求全国交通咨询模拟代码,要求如下,可以完全在dev c++运行
  • ¥15 根据要求修改程序编码
  • ¥15 用 Python 做一个用 Excel 表导入的答题系统
  • ¥15 使用微信开发者工具实现一个“婚博会”小程序
  • ¥15 ros的rviz仿真机器人
  • ¥15 关于#linux#的问题(输入输出错误):出现这个界面接着我重新装系统,又让修电脑的师傅帮我扫描硬盘(没有问题)用着用着又卡死(相关搜索:固态硬盘)