あなたの投稿の冒頭は紛らわしいです: あなたの文には、「状態を変更している間、都市はチェックボックスで表示する必要があります。以下は私のコードです」〜状態はチェックボックスまたは選択する必要がありますか?
いずれにせよ、私はこれを「選択」としての州と「チェックボックス」としての都市を持つ作業コードとして持っています:
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'];
}