【发布时间】:2015-05-07 14:00:21
【问题描述】:
我在 Drupal 7 中的 ajax 函数有一些问题。 当我使用 ajax 命令替换功能来替换表单文本字段时,它会失去其 ajax 功能。
例子:
表单构建:
function ds_check_post_new_data_import($form, &$form_state) {
$form['txt1'] = array(
'#type' => 'textfield',
'#size' => 2,
'#value' => "val1",
'#name' => "txt1",
'#ajax' => array(
'callback' => 'ajax_test_callback2',
),
'#prefix' => '<div id="txt1">',
'#suffix' => '</div>',
);
$form['txt2'] = array(
'#type' => 'textfield',
'#size' => 2,
'#value' => "val2",
'#name' => "txt2",
'#ajax' => array(
'callback' => 'ajax_test_callback2',
),
'#prefix' => '<div id="txt2">',
'#suffix' => '</div>',
);
return $form;
}
ajax 回调:
function ajax_test_callback2($form, &$form_state) {
$form['txt1'] = array(
'#type' => 'textfield',
'#size' => 2,
'#value' => "ajaxed1",
'#name' => "txt1",
'#ajax' => array(
'callback' => 'ajax_test_callback2',
),
'#prefix' => '<div id="txt1">',
'#suffix' => '</div>',
);
$form['txt2'] = array(
'#type' => 'textfield',
'#size' => 2,
'#value' => "ajaxed2",
'#name' => "txt2",
'#ajax' => array(
'callback' => 'ajax_test_callback2',
),
'#prefix' => '<div id="txt2">',
'#suffix' => '</div>',
);
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace("#txt1", render($form['txt1'])),
ajax_command_replace("#txt2", render($form['txt2']))
)
);
}
现在,当我更改第一个文本字段中的值时,两者都会被替换,这很好。但是当我再次更改该值时,ajax 功能就消失了。
有人遇到同样的问题吗?
在你问我为什么要这样做之前:
实际上我有一个自定义表格主题,它呈现一些行,并且在每一行上都有三个文本字段(使用自定义主题表格呈现..) 当用户在第一个文本框中更改某些内容时,其他两个必须根据输入的值进行更改。 然后当用户更改第二个文本字段中的值时,其他两个必须更新。
感谢阅读:)
【问题讨论】: