我正在研究drupal 7。下面是代码。我所做的是选择汽车型号汽车变体将改变并将数据保存在表格中。
功能 add_offer_form($form, $formstate) {
$form['add_offer_new_car_model'] = 数组(
'#type' => 'select',
'#required' => TRUE,
'#options' => $car_model,
'#ajax' => array(
'effect' => 'fade',
'progress' => array('type' => 'none'),
'callback' => 'variant_callback',
'wrapper' => 'replace_variant',
),
);
// 用于选择新车变体的组合框
$form['add_offer_new_car_variant'] = 数组(
'#type' => 'select',
'#options' => array(),
// The prefix/suffix provide the div that we're replacing, named by #ajax['wrapper'] above.
'#prefix' => '<div id="replace_variant">',
'#suffix' => '</div>',
);
// AJAX 请求为每次更改调用表单构建器函数。
// 我们可以根据 $form_state 更改构建表单的方式。
if (!empty($formstate['values']['add_offer_new_car_model'])) {
$model_id = $formstate['values']['add_offer_new_car_model'];
$rows = array();
$result = db_query("SELECT id, variant_name from {va_car_variant} where car_model_id in ($model_id,1) order by variant_name");
while ($data = $result->fetchObject()) {
$id = $data->id;
$rows[$id] = $data->variant_name;
}
$form['add_offer_new_car_variant']['#options'] = $rows;
}
}
///////////////////////////////////////// ////// ///////// AJAX 回调函数
函数变体回调($form,&$form_state){
返回 $form['add_offer_new_car_variant'];
}