ajax 以drupal 7形式api将参数传递给ajax回调函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6088455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pass arguments to ajax callback function in drupal 7 form api
提问by user764851
how to pass arguments to Ajax callback function in drupal 7 form api
如何在drupal 7表单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 ..
}
for example if i need to specify form element to make action using ajax i need to pass the element name to the function and make customer operation and return the result to another element form
例如,如果我需要指定表单元素以使用 ajax 进行操作,我需要将元素名称传递给函数并进行客户操作并将结果返回到另一个元素表单
i need passed aruguments to this callback function 'callback' => 'ajax_function_name_callback'
我需要将参数传递给这个回调函数 'callback' => 'ajax_function_name_callback'
function ajax_function_name_callback($args1,$args2,...$form,$form_state) { return .. }
function ajax_function_name_callback($args1,$args2,...$form,$form_state) { return .. }
2 - and how Through the form ?
2-如何通过表格?
thanks..
谢谢..
if i dont know what the $input_name it's genrated from something oprations i need to tell ajax_'function_name_callback the name of this field to make
如果我不知道 $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];
}
回答by caboose
Use this $form_state['triggering_element']this will tell you the name of the triggering element and give you all of its attributes.
使用它$form_state['triggering_element']这将告诉您触发元素的名称并为您提供其所有属性。
回答by Berdir
Through the form. No need for a custom callback.
通过表格。不需要自定义回调。
The ajax callback has access to all information that is part of the form. You can for example add a form element with type hidden (sent to the browser, can be changed with JavaScript for example) or value (only used internally, can contain any kind of data like objects and can not be changed by the user).
ajax 回调可以访问表单中的所有信息。例如,您可以添加一个隐藏类型的表单元素(发送到浏览器,例如可以用 JavaScript 更改)或值(仅在内部使用,可以包含任何类型的数据,如对象,用户不能更改)。
If you can give a more detailed example of what you want to do, I can give you a more detailed explanation.
如果你能给出一个更详细的例子你想做什么,我可以给你一个更详细的解释。
回答by Oleg Sherbakov
There is no way to pass additional arguments in ajax callback function.
没有办法在 ajax 回调函数中传递额外的参数。
See includes/form.inc on line 381 for more details.
有关更多详细信息,请参阅第 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!
}
}
回答by shivaP
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
Enjoy the coding :)
享受编码:)
回答by user3210341
i also have the same problem. My solution for that is to define a hidden field for the form and then get the hidden value in the callback. For example:
我也有同样的问题。我的解决方案是为表单定义一个隐藏字段,然后在回调中获取隐藏值。例如:
$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,
);
And then in the callback:
然后在回调中:
function ajax_example_autocheckboxes_callback($form, $form_state) {
$token=$form_state['values']['skybird_token'];
}
回答by Valentine94
Some conclusion of a possible ways to passing variables into the AJAX-callback:
将变量传递到 AJAX 回调的可能方法的一些结论:
- Save your variable to the $form_state['YOUR_VAR_NAME'], and then use in callback.
- Create a form element with a hidden type like proposed before.
Save your variable into a some of form element attributes like:
$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]; }
- 将变量保存到 $form_state['YOUR_VAR_NAME'],然后在回调中使用。
- 像之前建议的那样,创建一个具有隐藏类型的表单元素。
将变量保存到一些表单元素属性中,例如:
$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]; }
回答by shahab
you can use $form_state and pass it to callback function, for example
例如,您可以使用 $form_state 并将其传递给回调函数
define in form function like this
像这样定义表单函数
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 ..
}
pay attention if you want to change form_state['var'] in ajax_function_name_callback you must use
注意如果你想改变 ajax_function_name_callback 中的 form_state['var'] 你必须使用
function ajax_function_name_callback($form, &$form_state)
instead of
代替
function ajax_function_name_callback($form, $form_state)

