简体   繁体   English

如何在输入/文本框内使用JavaScript在不实际触摸键盘的情况下按Enter键

[英]How to press Enter without actually touching keyboard, using JavaScript, inside an input/textbox box

I know this has been asked many times and I searched a lot. 我知道这个问题已经被问了很多遍了,我进行了很多搜索。 But solutions are either not working in IE or not meeting my requirements. 但是解决方案要么无法在IE中运行,要么无法满足我的要求。

I have an input box where user can input the name. 我有一个输入框,用户可以在其中输入名称。 The value of input box gets saved into an array split by space. 输入框的值将保存到按空格分割的数组中。

Now I am copying array[0] to input box 1, array[1] to input box 2 ans so on. 现在,我将array [0]复制到输入框1,将array [1]复制到输入框2等等。

<input id="txbox" type="text" value="Hello World" /> 

array[0]=Hello 
array[1]=World 

<input id="input1" type="text" value="Hello" style="display:none"/> 
<input id="input2" type="text" value="World"  style="display:none"/> 

As soon as the text gets copied, I want to hit/trigger ENTER inside the input area just after the text, without actually touching the keyboard (through JavaScript). 一旦文本被复制,我想在文本之后立即在输入区域内点击/触发ENTER,而无需实际触摸键盘(通过JavaScript)。 How can I achieve that? 我该如何实现?

PS: the solution should work in IE8 and above and should be in plain JavaScript not jQuery. PS:该解决方案应在IE8及更高版本中运行,并且应使用纯JavaScript而不是jQuery。

Need of Simulating Enter: 需要模拟输入:

I am using below code in my project. 我在我的项目中使用以下代码。 which converts input text from English to Hindi only on pressing enter just after the word. 仅在单词后面按Enter时,才将输入文本从英语转换为印地语。 By this I can transliterate word by word. 这样我就可以逐字地音译。

    <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 type="text/javascript">

      // Load the Google Transliterate API
      google.load("elements", "1", {
            packages: "transliteration"
          });

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

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

        // Enable transliteration in the textbox with id
        // 'transliterateTextarea'.
        control.makeTransliteratable(['txtEnglish']);
      }
      google.setOnLoadCallback(onLoad);
    </script>
  </head>
  <body>
    Type in Hindi (Press Ctrl+g to toggle between English and Hindi)<br>
    <input type="text" id="txtEnglish" size="20"/>
    <input type="text id="txtHindi" size="20"  readonly/>
  </body>
</html> 

But, I have requirement like, user should enter the name in English and it should converts to Hindi and should display in other textbox which is readonly. 但是,我有这样的要求,用户应输入英文名称,然后应将其转换为印地语,并应显示在其他只读文本框中。

create a keypress event 创建按键事件

here's my sample code in my app. 这是我的应用程序中的示例代码。 Hope it helps. 希望能帮助到你。

Keycode 13 is the Enter key 键码13是Enter键

 $('.search-input').keypress(function(event) {
          var search = $('.search-input').val();
          var keycode = (event.keyCode ? event.keyCode : event.which);
          if (keycode == '13') {
              var len = $('.search-input').val().length;

              if (len <= 0) {

                  $('#search-contents').slideDown();
                  $('.item-list').append('<div class="col-centered"><h3 class="title"> No Item/s Found.</h3></div>');
              } else {

                  $('#search-contents').slideDown();
                  $('html,body').animate({
                          scrollTop: $('#search-contents').offset().top
                      },
                      'slow');

                  searchFunction(search);


              }

          }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JavaScript在TextBox中按Enter键 - Press Enter Key using JavaScript Inside a TextBox Javascript触发Enter键,而在载入搜索输入页面时并未实际按下Enter键 - Javascript triger an Enter key without actually press the Enter key when onload page for search input laravel-如何触发输入文本框的按键以实现javascript代码 - laravel - How to trigger enter key press of textbox to implement javascript code 如何在jQuery + JavaScript中的输入框中仅输入数字(使用正则表达式)? - how to enter only number in input box(using regex) in jquery + javascript? 如何在键盘“输入”按下时触发 :active 伪类? (仅使用 CSS) - How to trigger the :active pseudoclass on keyboard 'enter' press? (using only CSS) 如何使用JavaScript更新按键上的文本框 - How to update a textbox on key press using JavaScript 当您按Tab时,则应在不使用标签索引的情况下跳至下一个空白文本输入框javascript - when you press tab then it should tab to next blank text input box javascript without using tab index BootstrapVue 消息框输入按 `Enter` 确认 - BootstrapVue message box input press `Enter` to confirm 在输入框内按下键 - press key inside an input box 如何触发文本框中的输入事件,而无需在javascript中按Enter - how to trigger enter event in text box without pressing enter in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM