2

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

$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>',
);

////////// on changege 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' => 'select' ' 而不是“'#type' => 'checkboxes' ”,它工作正常。但我需要 tis 作为复选框。我在这里错了。请帮我。

4

1 回答 1

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'];
}

或者,如果您希望 state 成为“复选框”以及城市,您可以这样做:

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'];
}
于 2013-05-31T14:05:53.473 回答