音译不是自动完成的

时间:2018-06-26 09:34:19

标签: javascript jquery transliteration

我正在使用下面发布的代码来transliterate从英语到乌尔都语和阿拉伯语的输入。当我在输入字段(英文文本)中按回车按钮时,音译应该自动完成,但这没有发生。但是,如果我在乌尔都语和阿拉伯语字段中手动点击输入按钮,则它们的文本将被音译。但是请告诉我如何获得所需的行为,即当我在“英语”字段内按Enter键时,输入的文本应在乌尔都语阿拉伯语字段内自动音译?

<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script type="text/javascript" src="https://www.google.com/jsapi">
  </script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script type="text/javascript">
    // Load the Google Transliterate API
    google.load("elements", "1", {
      packages: "transliteration"
    });

    function onLoad() {
      var options_u = {
        sourceLanguage: google.elements.transliteration.LanguageCode.ENGLISH,
        destinationLanguage: [google.elements.transliteration.LanguageCode.URDU],
        shortcutKey: 'ctrl+g',
        transliterationEnabled: true
      };

      var options_a = {
        sourceLanguage: google.elements.transliteration.LanguageCode.ENGLISH,
        destinationLanguage: [google.elements.transliteration.LanguageCode.ARABIC],
        shortcutKey: 'ctrl+g',
        transliterationEnabled: true
      };

      // Create an instance on TransliterationControl with the required
      // options.
      var control_u = new google.elements.transliteration.TransliterationControl(options_u);
      var control_a = new google.elements.transliteration.TransliterationControl(options_a);

      control_u.makeTransliteratable(['txtUrdu']);
      control_a.makeTransliteratable(['txtArabic']);

      $("#txtEnglish").on('keydown', function(event) {
        if (event.keyCode == 13) {
          $("#txtUrdu").val($("#txtEnglish").val());
          $("#txtUrdu").focus();
          $("#txtUrdu").trigger(jQuery.Event('keydown', {
            keyCode: 13
          }));

          $("#txtArabic").val($("#txtEnglish").val());
          $("#txtArabic").focus();
          $("#txtArabic").trigger(jQuery.Event('keydown', {
            keyCode: 13
          }));
        }
      });
    } //end onLoad function

    google.setOnLoadCallback(onLoad);
  </script>
</head>

<body>
  English: <input type="text" id="txtEnglish" /> <br/> Urdu: <input type="text" id="txtUrdu" /><br/> Arabic: <input type="text" id="txtArabic" />
</body>

</html>

0 个答案:

没有答案