【问题标题】:How to use enter button from keyboard as submit如何使用键盘上的输入按钮作为提交
【发布时间】:2019-12-27 07:44:11
【问题描述】:

我正在使用 javaScript、HTML 和 CSS 制作一个待办事项列表,并且要插入任何元素,我们必须单击提交(添加)按钮,但我希​​望当我们从键盘按下输入按钮时,元素将插入或提交按钮会按,我不知道该怎么做,因为我是 javascript 新手


这是代码


HTML

<DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
</head>
<body>
<input id="task"><button id="add">Add</button>
<hr>
<div id="todos"></div>

<script src="nv.js"></script>
</body>
</html>

Java 脚本

function get_todos() {
  var todos = new Array;
  var todos_str = localStorage.getItem('todo');
  if (todos_str !== null) {
      todos = JSON.parse(todos_str); 
  }
  return todos;
}

function add() {
  var task = document.getElementById('task').value;

  var todos = get_todos();
  todos.push(task);
  localStorage.setItem('todo', JSON.stringify(todos));

  show();

  return false;
}

function remove() {
  var id = this.getAttribute('id');
  var todos = get_todos();
  todos.splice(id, 1);
  localStorage.setItem('todo', JSON.stringify(todos));

  show();

  return false;
}

function show() {
  var todos = get_todos();

  var html = '<ul>';
  for(var i=0; i<todos.length; i++) {
      html += '<li>' + todos[i] + '<button class="remove" id="' + i  + '">x</button></li>';
  };
  html += '</ul>';

  document.getElementById('todos').innerHTML = html;

  var buttons = document.getElementsByClassName('remove');
  for (var i=0; i < buttons.length; i++) {
      buttons[i].addEventListener('click', remove);
  };
}

document.getElementById('add').addEventListener('click', add);
show();

谢谢

【问题讨论】:

标签: javascript html visual-studio javascript-objects


【解决方案1】:

需要将onkeypress 事件添加到task 输入,如下所示:-

HTML

<input id="task" onkeypress="enterToSubmit(event)">

Javascript:

function enterToSubmit(event){
    var key = event.keyCode;
    if(key===13){
        add();
    }
}

【讨论】:

    【解决方案2】:

    您可以使用keydown 并在按下回车键时触发点击

    document.addEventListener('keydown', function(e) {
        if (e.keyCode == 13) {
             document.getElementById('add').clock();
        }
    });
    
    function get_todos() {
      var todos = new Array;
      var todos_str = localStorage.getItem('todo');
      if (todos_str !== null) {
          todos = JSON.parse(todos_str); 
      }
      return todos;
    }
    
    function add() {
      var task = document.getElementById('task').value;
    
      var todos = get_todos();
      todos.push(task);
      localStorage.setItem('todo', JSON.stringify(todos));
    
      show();
    
      return false;
    }
    
    function remove() {
      var id = this.getAttribute('id');
      var todos = get_todos();
      todos.splice(id, 1);
      localStorage.setItem('todo', JSON.stringify(todos));
    
      show();
    
      return false;
    }
    
    function show() {
      var todos = get_todos();
    
      var html = '<ul>';
      for(var i=0; i<todos.length; i++) {
          html += '<li>' + todos[i] + '<button class="remove" id="' + i  + '">x</button></li>';
      };
      html += '</ul>';
    
      document.getElementById('todos').innerHTML = html;
    
      var buttons = document.getElementsByClassName('remove');
      for (var i=0; i < buttons.length; i++) {
          buttons[i].addEventListener('click', remove);
      };
    }
    
    document.getElementById('add').addEventListener('click', add);
    show();
    <DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
    </head>
    <body>
    <input id="task"><button id="add">Add</button>
    <hr>
    <div id="todos"></div>
    
    <script src="nv.js"></script>
    </body>
    </html>

    【讨论】:

      【解决方案3】:

      只要用户在键盘上按下按钮,您就可以执行一个函数,例如

      <input id="myInput" value="Some text..">
      
      <script>
      var input = document.getElementById("myInput");
      input.addEventListener("keyup", function(event) {
        if (event.keyCode === 13) {
         event.preventDefault();
         document.getElementById("myBtn").click();
        }
      });
      </script>
      
      
      

      【讨论】:

        猜你喜欢
        • 2011-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多