Drupal 7复选框数组替换为ajax回调

时间:2013-04-04 09:02:57

标签: php drupal drupal-7

我将州字段作为下拉列表。在改变状态时,应在复选框中查看城市。以下是我的代码

$form['rate_the_service']['state'] = array(
  '#type' => 'select',
  '#prefix'=>'<div id="dropdown-third-replace">',
  '#suffix'=>'</div>',
  '#options'=>array(
    '0' => t('state1'),
    '1' => t('state2'),
    '2' => t('state3')
  ),
  '#attributes' => array(
    'selected' => array('selected')
  ),
  '#validated' => TRUE,
  '#ajax' => array(
    'callback' => 'ajax_example_dependent_dcheck_state_callback',
    'wrapper' => 'checkboxes-four-replace',
  ),
  '#suffix'=>'</div>',
);

因此,在更改状态时,城市将使用函数回调“ajax_example_dependent_dcheck_state_callback”显示在复选框中

以下是默认的城市代码:

$cityarray = array(
  '1' => 'chk1'
);

$form['rate_the_service']['city'] = array(
  '#prefix'=>'<div id="checkboxes-four-replace">',
  '#type' => 'checkboxes',
  '#options' => $cityarray,
  '#default_value' => isset($values['city']) ? $values['city'] : NULL,
  '#suffix'=>'</div></div>',
);

///////////关于更改ajax_example_dependent_dcheck_state_callback函数调用

function ajax_example_dependent_dcheck_state_callback($form, $form_state) {
  $state = $form_state['values']['state'];
  $city_array = array(
    '0' => 'city1',
    '2' => 'city2',
    '3' => 'city3'
  );
  $options[$state] = $city_array;
  $form['rate_the_service']['city']['#options'] = $city_array;
  return $form['rate_the_service']['city'];
}

在onchange状态下,'checkboxes-four-replace'将被city1,2,3复选框替换

onchange功能不起作用。如果我将类型更改为''#type'=&gt; 'select''而不是''#type'=&gt;'复选框'“它的工作正常。但我需要tis作为复选框。我在这里错了。请帮我。

1 个答案:

答案 0 :(得分:0)

你的帖子的开头很混乱:你的句子说明“在改变状态时,应该在复选框中查看城市。下面是我的代码”〜状态应该是复选框还是选择?

无论哪种方式,我将此作为工作代码,状态为“选择”,城市为“复选框”:

function state_form($form, &$form_state) {
  $form = array();

  $form['rate_the_service']['state'] = array(
    '#type' => 'select',
    '#prefix'=>'<div id="dropdown-third-replace">',
    '#suffix'=>'</div>',
    '#options'=>array(
      '0' => t('Ohio'),
      '1' => t('New York'),
    ),
    '#validated' => TRUE,
    '#ajax' => array(
      'event' => 'change',
      'callback' => 'ajax_example_dependent_dcheck_state_callback',
      'wrapper' => 'checkboxes-four-replace',
    ),
  );

  $city_array = array(
    '1' => array(
      '1' => 'Cincinnati',
      '2' => 'Cleveland',
      '3' => 'Toledo'
    ),
    '2' => array(
      '4' => 'New York City',
      '5' => 'Buffalo',
      '6' => 'Syracuse'
    ),
  );

  if (isset($form_state['values']['state']) && $form_state['values']['state'] == 1) {
    $form['rate_the_service']['city'] = array(
      '#prefix'=>'<div id="checkboxes-four-replace">',
      '#type' => 'checkboxes',
      '#options' => $city_array[2],
      '#default_value' => isset($values['city']) ? $values['city'] : array('1'),
      '#suffix'=>'</div></div>',
    );
  } else {
    $form['rate_the_service']['city'] = array(
      '#prefix'=>'<div id="checkboxes-four-replace">',
      '#type' => 'checkboxes',
      '#options' => $city_array[1],
      '#default_value' => isset($values['city']) ? $values['city'] : array('2'),
      '#suffix'=>'</div></div>',
    );
  }
  return $form;
}

/**
 * Ajax Callback for development_form
 */
function ajax_example_dependent_dcheck_state_callback($form, $form_state) {
  return $form['rate_the_service']['city'];
}

或者,如果您希望州和“城市”一样成为“复选框”,则可以执行以下操作:

function state_form($form, &$form_state) {
  $form = array();

  $form['rate_the_service']['state'] = array(
    '#type' => 'checkboxes',
    '#prefix'=>'<div id="dropdown-third-replace">',
    '#suffix'=>'</div>',
    '#options'=>array(
      'ohio' => t('Ohio'),
      'new_york' => t('New York'),
    ),
    '#validated' => TRUE,
    '#ajax' => array(
      'event' => 'change',
      'callback' => 'ajax_example_dependent_dcheck_state_callback',
      'wrapper' => 'checkboxes-four-replace',
    ),
  );

  $city_array = array(
    '1' => array(
      '1' => 'Cincinnati',
      '2' => 'Cleveland',
      '3' => 'Toledo'
    ),
    '2' => array(
      '4' => 'New York City',
      '5' => 'Buffalo',
      '6' => 'Syracuse'
    ),
  );

  // refine as needed to fit your validation, if this is your use case
  if (isset($form_state['values']['state']['ohio']) && $form_state['values']['state']['ohio'] == 'ohio') {
    $form['rate_the_service']['city'] = array(
      '#prefix'=>'<div id="checkboxes-four-replace">',
      '#type' => 'checkboxes',
      '#options' => $city_array[1],
      '#default_value' => isset($values['city']) ? $values['city'] : array('1'),
      '#suffix'=>'</div></div>',
    );
  } else if (isset($form_state['values']['state']['new_york']) && $form_state['values']['state']['new_york'] == 'new_york') {
    $form['rate_the_service']['city'] = array(
      '#prefix'=>'<div id="checkboxes-four-replace">',
      '#type' => 'checkboxes',
      '#options' => $city_array[2],
      '#default_value' => isset($values['city']) ? $values['city'] : array('2'),
      '#suffix'=>'</div></div>',
    );
  } else {
    $form['rate_the_service']['city'] = array(
      '#prefix'=>'<div id="checkboxes-four-replace">',
      '#type' => 'checkboxes',
      '#options' => array(),
      '#default_value' => isset($values['city']) ? $values['city'] : array('2'),
      '#suffix'=>'</div></div>',
    );
  }
  return $form;
}

/**
 * Ajax Callback for development_form
 */
function ajax_example_dependent_dcheck_state_callback($form, $form_state) {
  return $form['rate_the_service']['city'];
}

以下是状态为“选择”框,城市为“选择”框

function state_form($form, &$form_state) {
  $form = array();

  $form['rate_the_service']['state'] = array(
    '#type' => 'select',
    '#prefix'=>'<div id="dropdown-third-replace">',
    '#suffix'=>'</div>',
    '#options'=>array(
      'ohio' => t('Ohio'),
      'new_york' => t('New York'),
    ),
    '#validated' => TRUE,
    '#ajax' => array(
      'event' => 'change',
      'callback' => 'ajax_example_dependent_dcheck_state_callback',
      'wrapper' => 'checkboxes-four-replace',
    ),
  );

  $city_array = array(
    '1' => array(
      '1' => 'Cincinnati',
      '2' => 'Cleveland',
      '3' => 'Toledo'
    ),
    '2' => array(
      '4' => 'New York City',
      '5' => 'Buffalo',
      '6' => 'Syracuse'
    ),
  );

  if (isset($form_state['values']['state']) && $form_state['values']['state'] == 'new_york') {
    $form['rate_the_service']['city'] = array(
      '#prefix'=>'<div id="checkboxes-four-replace">',
      '#type' => 'select',
      '#options' => $city_array[2],
      '#default_value' => isset($values['city']) ? $values['city'] : array('1'),
      '#suffix'=>'</div></div>',
    );
  } else {
    $form['rate_the_service']['city'] = array(
      '#prefix'=>'<div id="checkboxes-four-replace">',
      '#type' => 'select',
      '#options' => $city_array[1],
      '#default_value' => isset($values['city']) ? $values['city'] : array('2'),
      '#suffix'=>'</div></div>',
    );
  }
  return $form;
}

/**
 * Ajax Callback for development_form
 */
function ajax_example_dependent_dcheck_state_callback($form, $form_state) {
  return $form['rate_the_service']['city'];
}