如何使用Javascript / jQuery触发Drupal-Function?

时间:2013-05-05 00:04:29

标签: drupal drupal-7

我有一个具有不同值的复选框。当用户更改复选框时,我将触发Drupal-Function field_attach_update http://api.drupal.org/api/drupal/modules!field!field.attach.inc/function/field_attach_update/7

我知道如何使用jQuery检查复选框更改但是如何触发Drupal-Function呢?

1 个答案:

答案 0 :(得分:1)

您需要查看Form API ajax options。具体来说,我想你要定义一个调用field_attach_update的ajax ['callback']函数。

<?php
function my_form_func($form, $form_state) {
  $my_checkbox_val = isset($form_state['values']['my_checkbox']) ? $form_state['values']['my_checkbox'] : NULL;
  $form['my_checkbox'] = array(
    '#type' => 'checkbox',
    '#title' => t('Check me'),
    '#default_value' => $my_checkbox_val,
    '#return_value' => $nid, // Assuming you are working with a node, but could be any entity
    '#ajax' => array(
      'callback' => 'my_form_field_update_func',
      'event' => 'click',
    ),
  );
  return $form;
}

function my_form_field_update_func($form, $form_state) {
  if (isset($form_state['values']['my_checkbox'])) {
    $node = node_load($form_state['values']['my_checkbox']);
    field_attach_update('node', $node);
  }
  return $form['my_checkbox'];
}
?>