選択リストからアイテムを選択した後、テキスト フィールドを自動入力しようとしています。つまり、最初にユーザーに選択リストからアイテムを選択してもらい、次にさらに 3 つのテキスト フィールドがあり、選択されたものに基づいて個別の値を与えたいということです。
1 に答える
0
Drupal の「Ajax フレームワーク」を使用する必要があります。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 フレームワークのリンクを支援しています。
于 2016-12-24T19:08:18.553 に答える