2012-02-25 54 views

回答

2

是的,它可以用Ajax完成。以下代碼會在文本字段更新時自動更新總數:

function gender_total_menu() 
{ 
    $items = array(); 

    $items['test'] = array(
     'title'   => Gender total, 
     'page callback' => 'drupal_get_form', 
     'page arguments' => array('gender_total_form'), 
     'access callback' => array(TRUE), 
     'type'   => MENU_CALLBACK, 
    ); 

    return $items; 
} 

function gender_total_form($form, &$form_state) 
{ 
    $total = 0; 

    if (array_key_exists('values', $form_state) && 
     array_key_exists('field_num_males', $form_state['values']) && 
     array_key_exists('field_num_females', $form_state['values']) 
    ) { 
     $total = $form_state['values']['field_num_males'] + $form_state['values']['field_num_females']; 
    } 

    $form = array(); 

    $form["field_num_males"] = array(
     '#type'   => 'textfield', 
     '#title'   => t("Number of males"), 
     '#default_value' => 0, 
     '#ajax' => array(
      'callback' => 'ajax_update_callback', 
      'wrapper' => 'wrapper', 
     ), 
    ); 

    $form["field_num_females"] = array(
     '#type'   => 'textfield', 
     '#title'   => t("Number of females"), 
     '#default_value' => 0, 
     '#ajax' => array(
      'callback' => 'ajax_update_callback', 
      'wrapper' => 'wrapper', 
     ), 
    ); 

    $form['total'] = array(
     '#markup' => '<p> Total: ' . $total . '</p>', 
     '#prefix' => '<div id="wrapper">', 
     '#suffix' => '</div>', 
    ); 

    return $form; 
} 

function ajax_update_callback($form, $form_state) 
{ 
    return $form['total']; 
} 
0

嘗試Computed Field模塊

計算字段是一個非常強大的CCK字段模塊,可讓您添加 定製的「計算域」到您的內容類型。這些計算的字段 填充了您通過PHP代碼定義的值。你可以在任何可用的Drupal上繪製 ,包括其他字段...

+0

它是否在填寫表單時填充字段?我認爲計算的字段是隱藏的,並保存在數據庫中.... – 2012-02-25 14:44:40

相關問題