【问题标题】:pass arguments to ajax callback function in drupal 7 form api以drupal 7表单api将参数传递给ajax回调函数
【发布时间】:2011-05-22 14:13:50
【问题描述】:

如何在 drupal 7 form api 中将参数传递给 Ajax 回调函数

$element['field name'] = array(
        '#type' => 'textfield',
        '#ajax' => array(
          'callback' => 'ajax_function_name_callback'/%/%/%,
          'method' => 'replace',
          'event' => 'blur',
              'effect' => 'fade',
              'progress' => array('type' => 'throbber', 'message' => ''),
        ),
    );

function ajax_function_name_callback($form,$form_state)
{
return ..
}

例如,如果我需要指定表单元素以使用 ajax 进行操作,我需要将元素名称传递给函数并进行客户操作并将结果返回到另一个元素表单

我需要将参数传递给这个回调函数 'callback' => 'ajax_function_name_callback'

函数 ajax_function_name_callback($args1,$args2,...$form,$form_state) { return .. }

2 - 又如何通过表格?

谢谢..

如果我不知道 $input_name 它是从某些操作中生成的,我需要告诉 ajax_'function_name_callback 这个字段的名称来生成

$element[$input_name] = array(
        '#type' => 'textfield',
        '#size' => '41',
        '#ajax' => array(
//////////////////////////////////////////////////////////////////////////////////
// here how i tell the ajax call back about this arguments informationvlike parents of this field ... etc
/////////////////////////////////////////////////////////////////////////////////
              'callback' => 'ajax_'function_name_callback',
          'method' => 'replace',
          'event' => 'blur',
              'effect' => 'fade',
              'progress' => array('type' => 'throbber', 'message' => ''),
        ),
    );


function ajax_'function_name_callback($arg_position,$arg_fieldName,$form,$form_state)
{
$form[$arg_position][$arg_fieldName][#value] = anotherFunction($form[$arg_position][$arg_fieldName][#value]);
return $form[$arg_position][$arg_fieldName];
}

【问题讨论】:

    标签: ajax drupal drupal-7


    【解决方案1】:

    使用$form_state['triggering_element'] 这将告诉您触发元素的名称并为您提供其所有属性。

    【讨论】:

    【解决方案2】:

    通过表格。无需自定义回调。

    ajax 回调可以访问表单中的所有信息。例如,您可以添加类型为 hidden(发送到浏览器,例如可以使用 JavaScript 更改)或 value(仅在内部使用,可以包含任何类型的数据,如对象,用户无法更改)的表单元素。

    如果你能给出一个更详细的例子来说明你想做什么,我可以给你一个更详细的解释。

    【讨论】:

    • 我需要将参数传递给这个回调函数 'callback' => 'ajax_function_name_callback'
    • 函数 ajax_function_name_callback($args1,$args2,...$form,$form_state) { return .. }
    • 是的,什么论据,它们从何而来?正如我所说,您应该通过表单结构传递它们。
    • mmmm 我还不明白你...请给我一个例子如何将参数传递给 ajax 回调函数
    • 如果告诉我你到底想做什么:) 你想通过什么?用户必须输入的东西?还是您在构建表单时知道的?
    【解决方案3】:

    无法在 ajax 回调函数中传递额外的参数。

    有关详细信息,请参阅第 381 行的 includes/form.inc。

    function ajax_form_callback() {
      list($form, $form_state) = ajax_get_form();
      drupal_process_form($form['#form_id'], $form, $form_state);
    
      // We need to return the part of the form (or some other content) that needs
      // to be re-rendered so the browser can update the page with changed content.
      // Since this is the generic menu callback used by many Ajax elements, it is
      // up to the #ajax['callback'] function of the element (may or may not be a
      // button) that triggered the Ajax request to determine what needs to be
      // rendered.
      if (!empty($form_state['triggering_element'])) {
        $callback = $form_state['triggering_element']['#ajax']['callback'];
      }
      if (!empty($callback) && function_exists($callback)) {
        return $callback($form, $form_state); // TA-DAM!
      }
    }
    

    【讨论】:

      【解决方案4】:
      The #ajax has an element called parameters, and can use as follows,
      
      
      $form['pro_div6_'.$id] = array(
                                  '#type' => 'button',
                                  '#value' => t('order now'),
                                  '#ajax' => array(
                                        'wrapper' => 'order_wrapper',
                                       'callback' => 'product_order_callback',
                                       'parameters'=>array('param1'=>'param1_vale','param2'=>'param2_vale','param3'=>'param3_vale'),
      
                                       ),
      
                          );
      And you can get this values from the call back function as,
        function product_order_callback($form, $form_state){
      
       echo "<pre>";print_r($form_state['triggering_element']); exit;
      
        }
      
      
       will get an output like,
      ![Please find the screen shot][1]
      
      
        [1]: http://i.stack.imgur.com/75PoU.jpg
      

      享受编码:)

      【讨论】:

        【解决方案5】:

        我也有同样的问题。我的解决方案是为表单定义一个隐藏字段,然后在回调中获取隐藏值。例如:

        $form['departure_city_1']= array(
            '#type' => 'select',
            '#options'=>$destination_array,
            '#weight'=>1,
            '#ajax' => array(
                'callback' => 'ajax_example_autocheckboxes_callback',
                'wrapper' => 'checkboxes-div',
                'method' => 'replace',
                'effect' => 'fade',
            ),
        
        );
        
        $form['skybird_token']= array(
            '#type' => 'hidden',
            '#value' => $token,
        );
        

        然后在回调中:

        function ajax_example_autocheckboxes_callback($form, $form_state) {
             $token=$form_state['values']['skybird_token'];
        }
        

        【讨论】:

          【解决方案6】:

          将变量传递到 AJAX 回调的可能方法的一些结论:

          1. 将变量保存到 $form_state['YOUR_VAR_NAME'],然后在回调中使用。
          2. 创建一个具有隐藏类型的表单元素,就像之前建议的那样。
          3. 将您的变量保存到一些表单元素属性中,例如:

            
            $element['field_name'] = array(
              '#type' => 'textfield',
              '#ajax' => array(
                'callback' => 'ajax_function_name_callback',
                'method'   => 'replace',
                'event'    => 'blur',
                'effect'   => 'fade',
                'progress' => array(
                  'type'    => 'throbber',
                  'message' => '',
                ),
              ),
              '#attributes' => array(
                'data' => array('some_data'),
                'id'   => array('some_id'),
              ),
            );
            
            function ajax_function_name_callback($form,$form_state) {
              $data = $form['field_name']['#attributes']['data'][0];
              $id = $form['field_name']['#attributes']['id'][0];
            }
            

          【讨论】:

            【解决方案7】:

            您可以使用 $form_state 并将其传递给回调函数,例如

            这样定义表单函数

            function form_function_name($form, $form_state){
            
            $element['field name'] = array(
                '#type' => 'textfield',
                '#ajax' => array(
                  'callback' => 'ajax_function_name_callback',
                  'method' => 'replace',
                ),
            );
            
            $form_state['var'] = array('variable' => 'My_costome_variable'); 
            return $element;
            }
            
            function ajax_function_name_callback($form, $form_state){
            
            # use of $form_state['var']; 
            return ..
            }
            

            注意如果你想在 ajax_function_name_callback 中改变 form_state['var'] 你必须使用

            function ajax_function_name_callback($form, &$form_state)
            

            而不是

            function ajax_function_name_callback($form, $form_state)
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-09-20
              • 1970-01-01
              相关资源
              最近更新 更多