简体   繁体   English

单击时输入输入值或输入按下

[英]Accessing input value on click or enter press

I'm trying to simply return a value entered into an input field. 我试图简单地返回输入到输入字段中的值。

I want to use jQuery to access the value, store to a variable, and return it from a called function. 我想使用jQuery访问值,存储到变量,并从被调用的函数返回它。

Here is my HTML: 这是我的HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>Untitled</title>
    <link rel="stylesheet" type="text/css" href="stylesheet.css"/>
    <script language = "javascript" type = "text/javascript" src="jquery-2.1.1.js">      </script>
    <script language = "javascript" type = "text/javascript" src = "Database.js"></script>
  </head>
  <body>
      <div id="wrapper">
          <input type ="text" id="input"/></br>
          <input type="button" id="insert"/>
      </div>
  </body>
</html>

Here is my Javascript: 这是我的Javascript:

$(document).ready(function() {

var toSubmit = function() {
  var text = $("#input").val();
  return text;
};

var enterPressed = function() {
  $('#input').keyup(function(button) {
    if(button.keyCode==13) {
      toSubmit();
    }
  };

function enterPressed();

});

I haven't worked on the functionality of the button included, please ignore that. 我没有处理包含按钮的功能,请忽略它。

When I run this in Chrome's Javascript console I get "Unexpected token function for line 15. Am I incorrectly calling the function? 当我在Chrome的Javascript控制台中运行时,我得到“第15行的意外令牌功能。我是否错误地调用了该功能?

All I want the program to do is to return the value of the input box as soon as the user presses enter. 我想让程序做的就是在用户按下回车键后立即返回输入框的值。

Update 更新

HTML: HTML:

$(document).ready(function() {

var toSubmit = function() {
  var text = $("#input").val();
  return text;
};

var enterPressed = function() {
  $('#input').keyup(function(button) {
    if(button.keyCode==13) {
      toSubmit();
    }
  });
};

enterPressed();

});

Javascript: 使用Javascript:

$(document).ready(function() {

var values = [];

var toSubmit = function() {
  var text = $("#input").val();
  values.push(text);
  console.log(values);
  return false;
};

var formSubmit = function(e) {
  $('#form').submit(toSubmit);
};

});

As @DontVoteMeDown said in the comments, function enterPressed(); 正如@DontVoteMeDown在评论中所说, function enterPressed(); is a syntax error and you forgot to close enterPressed . 是语法错误,您忘记关闭enterPressed You also forgot to close the keyup with }); 你也忘了用});关闭keyup }); . I've corrected the errors in this code: 我已经纠正了此代码中的错误:

EDIT: Changed to event.which rather than event.keyCode ( which normalizes keyCode and charCode ). 编辑:更改为event.which而非event.keyCodewhich规范化keyCodecharCode )。

$(document).ready(function() {

  var toSubmit = function() {
    var text = $("#input").val();
    return text;
  };

  var enterPressed = function() {
    $('#input').keyup(function(event) {
      if(event.which == 13) {
        toSubmit();
      }
    });
  };

  enterPressed();

});

Working jsFiddle example: http://jsfiddle.net/D66Zw/1/ 工作jsFiddle示例: http//jsfiddle.net/D66Zw/1/

UPDATE: 更新:

If you can, it would be better if you just listened for a form's submit event. 如果可以的话,如果你只是听一个表单的submit事件会更好。 That way you can call toSubmit and it will handle the enter key press and the submit button. 这样你就可以调用toSubmit ,它将处理按下回车键和提交按钮。 Here is how you could do that: 这是你如何做到这一点:

JavaScript: JavaScript的:

$(document).ready(function() {

  var toSubmit = function(e) {
     var text = $("#input").val();

     // do whatever you want to do with 'text' variable here

     // stop form from submitting
     e.preventDefault();
     return false;
  };

  var formSubmit = function() {
      $('#form').submit(toSubmit);
  };

  // call this when you want to bind the submit handler
  formSubmit();

});

HTML: HTML:

<form id="form">
    <input type ="text" id="input"/><br />
    <input type="submit" id="insert"/>
</form>

New jsFiddle example, clicking the submit button and pressing enter works: http://jsfiddle.net/D66Zw/2/ 新的jsFiddle示例,单击提交按钮并按Enter键工作: http//jsfiddle.net/D66Zw/2/

Pushing value to array 将值推送到数组

Before the toSubmit function, create the array: toSubmit函数之前,创建数组:

var list = [];

In the toSubmit function: toSubmit函数中:

// push text to array
list.push(text);

// output array in console
console.log(list);

jsFiddle: http://jsfiddle.net/D66Zw/4/ (open up the console to see the array every time enter is pressed) jsFiddle: http//jsfiddle.net/D66Zw/4/ (打开控制台,每按一次输入就看到数组)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM