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 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办