无法通过回车键提交ajax自动填充表单

时间:2022-11-24 10:19:02

I'm trying to add feature to our corporate website (this module called 'userpasswords2' searches database of local mailsystem passwords). I'm using AJAX autocomplete and modification of the form.

我正在尝试向我们的公司网站添加功能(这个名为'userpasswords2'的模块搜索本地邮件系统密码的数据库)。我正在使用AJAX自动完成和修改表单。

I cannot submit the form by enter key. AJAX autocomplete works fine, but when I choose the user, the form can only be submitted by submit button.

我无法通过输入密钥提交表单。 AJAX自动完成工作正常,但是当我选择用户时,表单只能通过提交按钮提交。

I would like it to work like here https://api.drupal.org/api/drupal - user enters for example 'hook', chooses for example hook_menu, hits enter, then hits enter again and get the result!

我希望它像这里一样工作https://api.drupal.org/api/drupal - 用户输入例如'hook',选择例如hook_menu,点击进入,然后再次点击进入并获得结果!

So again - clicking submit button works fine, hitting 'enter' key doesn't work.

所以再次 - 单击提交按钮工作正常,点击'输入'键不起作用。

Googled a lot, the workarounds I've found doesn't work for me. Please help.

谷歌搜索了很多,我发现的解决方法对我不起作用。请帮忙。

function userpasswords2_menu() {
  $items = array();
  $items['userpasswords2'] = array(
    'title' => 'User passwords',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('userpasswords2_form'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['userpasswords2/ajax_username_autocomplete_callback2'] = array(
    'page callback' => 'ajax_username_autocomplete_callback2',
    'type' => MENU_CALLBACK,
    'access callback' => TRUE,
  );
  return $items;
}

function userpasswords2_form($form, &$form_state) {
  $form = array();
  $form['user'] = array(
      '#type' => 'textfield',
      '#title' => t('Enter username'),
      '#autocomplete_path' => 'userpasswords2/ajax_username_autocomplete_callback2',
      '#executes_submit_callback' => 'TRUE',
  );
  $form['box'] = array(
    '#type' => 'markup',
    '#prefix' => '<div id="box">',
    '#suffix' => '</div>',
    '#markup' => '<br>',
  );   
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#ajax' => array(
      'callback' => 'userpasswords2_callback',
      'wrapper' => 'box',
    ),
    '#value' => t('Submit'),
);
  return $form;
}

function ajax_username_autocomplete_callback2($string = "") {
  $matches = array();
  if ($string) {
    $result = db_select('my_domain_passwords')
      ->fields('my_domain_passwords',array('fullname','password'))
      ->condition('fullname', db_like($string) . '%', 'LIKE')
      ->range(0,10)
      ->execute();
    foreach ($result as $user) {
      $form['custom']['username'] = $matches[$user->fullname] = check_plain($user->fullname);
      $form['custom']['password'] = check_plain($user->password);
    }
  }
  drupal_json_output($matches);
}

function userpasswords2_form_validate($form, &$form_state) {
  $username = $form_state['values']['user'];
  $matches = array();
  // select from database by fullname
  $result = db_select('my_domain_passwords')
    ->fields('my_domain_passwords', array('fullname'))
    ->condition('fullname', db_like($username), 'LIKE')
    ->range(0,1)
    ->execute()
    ->fetchField();

  if (!empty($username)) {
    $form_state['custom']['username'] = $result;
    $password = db_select('my_domain_passwords')
      ->fields('my_domain_passwords', array('password'))
      ->condition('fullname', db_like($username), 'LIKE')
      ->range(0,1)
      ->execute()
      ->fetchField();
    $form_state['custom']['password'] = $password;
  }
  return $form;
}

function userpasswords2_callback($form, &$form_state) {
  if ( (!empty($form_state['custom']['username'])) && (!empty($form_state['custom']['password'])) ) {
    $output_string = $form_state['custom']['username'] . " : " . $form_state['custom']['password'];
  } else {
    $output_string = "No such user: " . $form_state['values']['user'];
  }
  $username = $form_state['custom']['username'];
  $password = $form_state['custom']['password'];
  $element = $form['box'];
  $element['#markup'] = $output_string;
  return $element;
}

1 个解决方案

#1


0  

If you remove your autocomplete, you can be able to submit by press enter. So the issue is in the autocomplete function. You need to patch the misc/autocomplte.js. Following is the patch, check out the link for more details.

如果您删除自动填充功能,则可以按Enter键提交。所以问题在于自动完成功能。你需要修补misc / autocomplte.js。以下是补丁,请查看链接以获取更多详细信息。

case 13: // Enter.
case 27: // Esc.
    this.hidePopup(e.keyCode);
    if (e.keyCode == 13 && $(input).hasClass('auto_submit')) {
       input.form.submit();
    }

This could be help you https://www.drupal.org/project/drupal/issues/309088

这可以帮助您https://www.drupal.org/project/drupal/issues/309088

#1


0  

If you remove your autocomplete, you can be able to submit by press enter. So the issue is in the autocomplete function. You need to patch the misc/autocomplte.js. Following is the patch, check out the link for more details.

如果您删除自动填充功能,则可以按Enter键提交。所以问题在于自动完成功能。你需要修补misc / autocomplte.js。以下是补丁,请查看链接以获取更多详细信息。

case 13: // Enter.
case 27: // Esc.
    this.hidePopup(e.keyCode);
    if (e.keyCode == 13 && $(input).hasClass('auto_submit')) {
       input.form.submit();
    }

This could be help you https://www.drupal.org/project/drupal/issues/309088

这可以帮助您https://www.drupal.org/project/drupal/issues/309088