【发布时间】:2014-06-11 11:05:53
【问题描述】:
我希望能够计算已填充的文本输入的数量,并使用结果创建带有选项 0-[返回计数] 的下拉列表。我正在使用 Drupal 7 中的 ajax_command_replace 函数,但对任何其他方法的建议持开放态度。
我有一个测试,它计算填充的输入并显示结果,但我不知道如何使用它来填充选择。
非常感谢任何帮助。
用于表单元素。
$array = array_fill(0,5,'');
$form['test'] = array(
'#type'=> 'fieldset',
'#title' => 'TEST',
);
$form['test']['value']['#tree'] = TRUE;
foreach ($array as $key => $value) {
$form['test']['value'][$key] = array(
'#type' => 'textfield',
'#ajax' => array(
'callback' => 'test_callback',
),
);
}
$test = count(array_filter($array));
$form['test']['count'] = array(
'#suffix' => "<div id='testcount'>filled inputs = $test</div>",
);
和 ajax 回调
function test_callback($form, $form_state){
$text = count(array_filter($form_state['input']['value']));
$commands = array();
$commands[] = ajax_command_replace("#testcount", "<div id='testcount'>filled inputs = $text</div>");
return array('#type' => 'ajax', '#commands' => $commands);
}
谢谢
【问题讨论】:
标签: php jquery ajax forms drupal-7