在 Drupal Webforms 中将两个文本字段复制并连接到另一个文本字段

Copying and Concating Two Textfields to another in Drupal Webforms

我有两个选择作为类型和类别。第三个是名为 Index 的文本字段。类型的值为 12345,类别为 ABC, D, E.

我想自动获取 Index 中的值 4A1E 视情况而定,具体取决于用户的选择。

嘿,我试过了这个和它的工作原理,你可以使用 hook_form_alter、#after_build 和 jQuery。

首先创建包含所需字段的网络表单。 然后创建一个自定义模块并使用 hook_form_alter,我在下面添加了示例代码,您只需将 "hook" 替换为您的模块名称并将 "your_webform_id" 替换为您的网络表单 ID。

function hook_form_alter(&$form, &$form_state, $form_id){
    //to check your webform id.
    //drupal_set_message($form_id);

    if($form_id == "your_webform_id"){
        $form['#after_build'] = array('_webform_after_build_handler');
    }
}

function _webform_after_build_handler(&$form, &$form_state){
    //*change the webform field id's as per your form*

    drupal_add_js('jQuery(document).ready(function(){ 
    var typ = jQuery("#edit-submitted-type").val();
    var cat = jQuery("#edit-submitted-category").val();
    if(cat != "" && typ != ""){
            jQuery("#edit-submitted-index").val(typ + cat);
        }
    jQuery("#edit-submitted-category").change(function(e){
        var typ = jQuery("#edit-submitted-type").val();
        var cat = jQuery(this).val();
        if(cat != "" && typ != ""){
            jQuery("#edit-submitted-index").val(typ + cat);
        }
    });
    jQuery("#edit-submitted-type").change(function(e){
        var typ = jQuery(this).val();
        var cat = jQuery("#edit-submitted-category").val();
        if(cat != "" && typ != ""){
            jQuery("#edit-submitted-index").val(typ + cat);
        }
    });', 'inline');
    return $form;
}

希望对您有所帮助。