您帖子的开头令人困惑:您的句子指出“在更改状态时,应在复选框中查看城市。下面是我的代码”〜状态应该是复选框还是选择?
无论哪种方式,我都将其作为工作代码,状态为“选择”,城市为“复选框”:
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'];
}