我有两个从属下拉列表,一个用于国家,一个用于州。我在多步骤网络表单中多次使用该概念。当表单加载时,国家列表就可以了。在选择特定国家时,州列表也可以。现在问题来了:如果网络表单的同一部分中有一些必填字段,而用户没有填写该字段,则表单会刷新并丢失整个状态列表。此外,当移动到下一步(多步网络表单)并返回同一页面时,该值将丢失。但是在整个过程中,国家选择列表的值是保留的。不保留使用 ajax 生成的选择选项。提前致谢。下面是使用的代码。
$form['submitted']['employment_history']['employer_1']['address_of_employer']['country']['#ajax'] = array(
'callback' => 'my_custom_ajax_callback_for_employer_one',
'wrapper' => 'edit-submitted-employment-history-employer-1-address-of-employer-state',
'method' => 'replace',
);
/*
* Implements Ajax callback for populating list of provinces (Employer One).
*/
function my_custom_ajax_callback_for_employer_one($from, $form_state) {
$selected_country = $form_state['values']['submitted']['employment_history']['employer_1']['address_of_employer']['country'];
$states = location_get_provinces($selected_country);
$form['submitted']['employment_history']['employer_1']['address_of_employer']['state']= array(
'#type' => 'select',
'#options' => $states,
'#attributes' => array('id' => 'edit-submitted-employment-history-employer-1-address-of-employer-state'),
);
$form['rebuild'] = TRUE;
return $form['submitted']['employment_history']['employer_1']['address_of_employer']['state'];
}