drupal 7 ajax框架示例不起作用

时间:2015-08-15 04:42:20

标签: drupal-7 drupal-forms drupal-ajax

我想在用户个人资料页面中添加一个表单。所以我创建了user-profiled.tpl.php。在该模板中,我只是从ajax框架中复制了示例以进行测试。这是整个代码:     

function ajax_example_simplest($form, &$form_state) {
 $form = array();
 $form['changethis'] = array(
'#type' => 'select',
'#options' => array(
  'one' => 'one',
  'two' => 'two',
  'three' => 'three',
),
'#ajax' => array(
  'callback' => 'ajax_example_simplest_callback',
  'wrapper' => 'replace_textfield_div',
 ),
);


$form['replace_textfield'] = array(
'#type' => 'textfield',
'#title' => t("The default value will be changed"),
'#description' => t("Say something about why you chose") . "'" .
  (!empty($form_state['values']['changethis'])
  ? $form_state['values']['changethis'] : t("Not changed yet")) . "'",
'#prefix' => '<div id="replace_textfield_div">',
'#suffix' => '</div>',
);
  return $form;
 }

function ajax_example_simplest_callback($form, $form_state) {
 return $form['replace_textfield'];
}

我现在很困惑。单击复选框后,文本字段中没有任何更改。我的jquery版本是1.9。还有其他限制吗?

1 个答案:

答案 0 :(得分:0)

更改是在描述中而不是在textfield中(我先做了同样的错误)。但是我更新了代码,因此它也显示了'textfield'中的值。

function ajax_example_simplest($form, &$form_state)
{
    $form = array();

    $form['changethis'] = array(
        '#type' => 'select',
        '#options' => array(
            'one'   => 'one',
            'two'   => 'two',
            'three' => 'three',
        ),
        '#ajax' => array(
            'callback' => 'ajax_example_simplest_callback2',
            'wrapper'  => 'replace_textfield_div',
        ),
    );

    $number = (!empty($form_state['values']['changethis']) ? $form_state['values']['changethis'] : t("Not changed yet"));
    $form['replace_textfield'] = array(
        '#type'        => 'textfield',
        '#title'       => t("The default value will be changed"),

        // Not that value was changed in the description
        '#description' => t("Say something about why you chose") . " '" . $number . "'",

        // Also add next line if you want to see it in the text box
        '#value'       => $number,

        '#prefix'      => '<div id="replace_textfield_div">',
        '#suffix'      => '</div>',
    );

    return $form;
}

function ajax_example_simplest_callback2($form, $form_state) {
    return $form['replace_textfield'];
}