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 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现