2

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

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

4

1 回答 1

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'];
}
?>
于 2013-05-05T01:41:40.147 回答