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 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。
  • ¥15 已知隐函数其中一个变量τ的具体值,求另一个变量
  • ¥15 r语言Hurst指数
  • ¥15 Acrn IVSHMEM doorbell问题
  • ¥15 yolov5中的val测试集训练时数量变小问题
  • ¥15 MPLS/VPN实验中MPLS的配置问题
  • ¥15 materialstudio氢键计算问题
  • ¥15 echarts图表制作