我想在用户配置文件页面添加一个表单。所以我创建了user-profile .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。还有其他限制吗?
改变是在描述,而不是在文本字段(我做了同样的错误,你做了)。然而,我已经更新了代码,所以它也显示了"文本字段"的值。
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'];
}