表单中条件字段的最简单方法


20

获取基于另一字段的值来启用/禁用表单字段的javascript魔术的最简单方法是什么?听起来好像某个地方应该有助手,但是我找不到它。我正在寻找不限于节点的解决方案。


不确定这真的是Drupal的问题。这是一种JavaScript,应在Stack Overflow上询问。
Camsoft 2011年

3
我一直在寻找Drupal解决方案。我能够为此编写Javascript,但确实感觉应该以某种方式将其插入Form API。
Fuzzy76 2011年

这很酷。我认为在原始问题中不清楚。我不知道有任何模块可以做到这一点。
Camsoft 2011年

Answers:


18

神奇的是使用#ahah / #ajax属性与表单元素,这样你可以定义哪些应该触发的修改,应该怎么修改结果,再加上它使用jQuery无缝集成。

这是下面示例中的重要部分:

'#ajax' => array(
    'event' => 'change',
    'callback' => 'myajax_ajax_callback',
    'wrapper' => 'dropdown_second_replace',
),

这是一个显示具有两个下拉菜单的基于表单的页面的示例:第二个下拉菜单中的选项列表取决于第一个下拉菜单中的选择。

<?php

/**
 * Implementation of hook_menu().
 * Registers a form-based page that you can access at "http://localhost/myajax"
 */
function myajax_menu(){
    return array(
        'myajax' => array(
            'title' => 'A page to test ajax',
            'page callback' => 'drupal_get_form',
            'page arguments' => array('myajax_page'),
            'access arguments' => array('access content'), 
        )
    );
}



/**
 * A form with a dropdown whose options are dependent on a
 * choice made in a previous dropdown.
 *
 * On changing the first dropdown, the options in the second are updated.
 */
function myajax_page($form, &$form_state) {
    // Get the list of options to populate the first dropdown.
    $options_first = myajax_first_dropdown_options();

    // If we have a value for the first dropdown from $form_state['values'] we use
    // this both as the default value for the first dropdown and also as a
    // parameter to pass to the function that retrieves the options for the
    // second dropdown.
    $value_dropdown_first = isset($form_state['values']['dropdown_first']) ? $form_state['values']['dropdown_first'] : key($options_first);

    $form['dropdown_first'] = array(
        '#type' => 'select',
        '#title' => 'First Dropdown',
        '#options' => $options_first,
        '#default_value' => $value_dropdown_first,

        // Bind an ajax callback to the change event (which is the default for the
        // select form type) of the first dropdown. It will replace the second
        // dropdown when rebuilt
        '#ajax' => array(
            // When 'event' occurs, Drupal will perform an ajax request in the
            // background. Usually the default value is sufficient (eg. change for
            // select elements), but valid values include any jQuery event,
            // most notably 'mousedown', 'blur', and 'submit'.
            'event' => 'change',
            'callback' => 'myajax_ajax_callback',
            'wrapper' => 'dropdown_second_replace',
        ),
    );
    $form['dropdown_second'] = array(
        '#type' => 'select',
        '#title' => 'Second Dropdown',
        // The entire enclosing div created here gets replaced when dropdown_first
        // is changed.
        '#prefix' => '<div id="dropdown_second_replace">',
        '#suffix' => '</div>',
        // when the form is rebuilt during ajax processing, the $value_dropdown_first variable
        // will now have the new value and so the options will change
        '#options' => myajax_second_dropdown_options($value_dropdown_first),
        '#default_value' => isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '',
    );
    return $form;
}

/**
 * Selects just the second dropdown to be returned for re-rendering
 *
 * Since the controlling logic for populating the form is in the form builder
 * function, all we do here is select the element and return it to be updated.
 *
 * @return renderable array (the second dropdown)
 */
function myajax_ajax_callback($form, $form_state) {
    return $form['dropdown_second'];
}


/**
 * Helper function to populate the first dropdown. This would normally be
 * pulling data from the database.
 *
 * @return array of options
 */
function myajax_first_dropdown_options() {
    return array(
        'colors' => 'Names of colors',
        'cities' => 'Names of cities',
        'animals' => 'Names of animals',
    );
}


/**
 * Helper function to populate the second dropdown. This would normally be
 * pulling data from the database.
 *
 * @param key. This will determine which set of options is returned.
 *
 * @return array of options
 */
function myajax_second_dropdown_options($key = '') {
    $options = array(
        'colors' => array(
            'red' => 'Red',
            'green' => 'Green',
            'blue' => 'Blue'
        ),
        'cities' => array(
            'paris' => 'Paris, France',
            'tokyo' => 'Tokyo, Japan',
            'newyork' => 'New York, US'
        ),
        'animals' => array(
            'dog' => 'Dog',
            'cat' => 'Cat',
            'bird' => 'Bird'
        ),  
    );
    if (isset($options[$key])) {
        return $options[$key];
    }
    else {
        return array();
    }
}

这是根据其字段之一的值更改表单的正确方法。但是要隐藏/显示或启用/禁用字段,表单元素上的#states属性更容易。
皮埃尔·布伊

6

不会在条件字段模块做到这一点?

编辑节点时,将使用JavaScript动态显示和隐藏受控字段。


对于节点形式和CCK字段,是。但是我想要可以在其他情况下使用的东西。我会澄清我的问题。
Fuzzy76 2011年

3

您可以使用两种不同的系统:

  • #ahah /#ajax允许您使用AJAX提交表单并在服务器端重建它。当您实际上要添加表单元素时很有用,D6中的典型示例是upload.module。上面已经解释过了。
  • Drupal 7中的新增功能是#states系统,该系统使您可以执行基于其他元素的显示/隐藏/启用/禁用表单元素之类的操作。有关更多信息,请参见http://www.randyfay.com/node/58

1

最简单的方法是编写您自己的JavaScript并使用jQuery将事件处理程序附加到blur和focus事件。然后在触发回调时,根据逻辑禁用/启用字段。


如果他没有能力编写自己的jQuery?Drupal模块会比编码容易吗?-由于问题已得到澄清,因此我撤回评论。
解密

首先,我本人并不了解Conditional Fields模块,其次,像这样的模块将通过一些简单的客户端JS给他的项目增加多少开销?
Camsoft 2011年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.