2016-12-08 6 views
0

私は選択リストから項目を選択した後にテキストフィールドを自動入力しようとしています。つまり、まず選択リストから項目を選択して3つのテキストフィールドがあり、どれが選択されたかに基づいて異なる値を与えたいと思っています。drupal7のオートフィルフィールド

+0

具体的にお試しください。たぶんあなたが試したこととあなたが見つけた問題を追加してください。 – zuazo

+0

私は選択リストを持つコンテンツタイプを持っています。選択リストの場合、私は分類法を使用しました。選択リストは企業のリストで、その他のフィールドはテキストフィールドです。彼らは会社の場所、国、電話に関する情報を持っています。私が会社を選ぶと、自分の国と場所と電話がありますので、私はそれらを自動的に記入したいと思います。階層的なリストを使ってみましたが、リストが1つしかないのでうまくいきませんでした。私もオートフィルモジュールを試しましたが、それもうまくいきませんでした。 @ zuazo – Kris

答えて

0

Drupal "Ajax framework"を使用する必要があります。 hook_form_alter関数でフィールドを準備してください。

function hook_form_alter(&$form, &$form_state, $form_id) { 
    if (isset($form['type']) && $form['type']['#value'] . '_node_settings' == $form_id) { 
    $form['select_field'] = array(
     '#ajax' => array(
     'callback' => '_mymodule_ajax_example_simplest_callback', 
     'wrapper' => 'replace_textfield_div', 
     ), 
    ); 

    // This entire form element will be replaced with an updated value. 
    $form['textfield_to_autofill'] = array(
     '#prefix' => '<div id="replace_textfield_div">', 
     '#suffix' => '</div>', 
    ); 
    } 
} 
function _mymodule_ajax_example_simplest_callback(&$form, $form_state) { 
    // The form has already been submitted and updated. We can return the replaced 
    // item as it is. 
    $commands = array(); 
    if($form_state['values']['select_field'][LANGUAGE_NONE][0]['value'] == "some_value"){ 
    $form['textfield_to_autofill'][LANGUAGE_NONE][0]['value']['#value'] = "some_value"; 
    $commands[] = ajax_command_replace("#replace_textfield_div", render($form['textfield_to_autofill'])); 
    } 
    $page = array('#type' => 'ajax', '#commands' => $commands); 
    ajax_deliver($page); 
} 

ここにはajax frameworkのリンクがあります。

+0

偉大な答え、ありがとう! – Kris

関連する問題