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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上