【问题标题】:Count textfields filled to populate options in a select input in Drupal 7 form计算填充的文本字段以填充 Drupal 7 表单中选择输入中的选项
【发布时间】: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


    【解决方案1】:

    这似乎对我有用,相同的方法,但使用 render() 函数从回调中返回更新的表单元素。我希望它可以帮助别人。

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

    回调

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多