duanhuang2804 2014-06-11 11:05
浏览 39
已采纳

计算填充的文本字段以填充Drupal 7表单中的选择输入中的选项

I would like to be able to count the number of text inputs that have been filled and use the result to create a drop down with the options 0-[count returned]. I am using the ajax_command_replace function in Drupal 7 but open to suggestions of any other method.

I have a test which does count the inputs filled and displays the result but I cannot figure out how to then use this to populate a select.

Any help much appreciated.

for the form elements.

$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>",
);

and the ajax callback

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);
}

Thanks

  • 写回答

1条回答 默认 最新

  • dq804806 2014-06-12 16:16
    关注

    This seems to be working for me now, same method but using the render() function to return an updated form element from within the callback. I hope it helps someone else.

    $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',                      
                            ),
                        );
    
                    }
                    $options = range(count(array_filter($array)),0,1);
                    $form['test']['count'] = array(
                        '#type'=> 'select',
                        '#options' => $options,
                        '#prefix' => '<div id="testcount">',
                        '#suffix' => "</div>",
    
                    );
    

    Callback

    function test_callback($form, $form_state){
    $text = count(array_filter($form_state['input']['value']));
    $default = $form_state['input']['count'];
    $options = range($text,0,1);
    $form['test']['count'] = array(
        '#type'=> 'select',
        '#options' => $options,
        '#prefix' => '<div id="testcount">',
        '#suffix' => "</div>",
        '#default_value' => $default,
    );
    
       $commands = array();
       $commands[] = ajax_command_replace("#testcount", render($form['test']['count']));
    
       return array('#type' => 'ajax', '#commands' => $commands);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?