OnKeydown未捕获第一个字符

时间:2015-07-18 10:58:48

标签: jquery

我想从文本框中捕获事件数据,但是对于输入的文本,它返回空。你能告诉我这里我做错了什么吗?输入的第一个字符不会提醒姓名。请帮忙。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script>
        $(function() {
            $("#name").keydown(function(event){
                var name = $("#name").val();
                alert("name" + name);
            });
        });
    </script>
</head>
<body>
    <input id="name">
</body>
</html>

2 个答案:

答案 0 :(得分:1)

|Starting dbm-update for database root @ jdbc:mysql://localhost/test?useUnicode=yes&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
Error |
2015-07-18 12:41:10,184 [main] ERROR liquibase  - Change Set Update4.groovy::1437215773652-1::mg (generated) failed.  Error: Error executing SQL CREATE TABLE `city` (`id` VARCHAR(16) NOT NULL, `version` BIGINT NOT NULL, `date_created` DATETIME NOT NULL, `last_updated` DATETIME NOT NULL, `name` VARCHAR(255) NOT NULL, CONSTRAINT `cityPK` PRIMARY KEY (`id`)) type=InnoDB: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 1
Message: Error executing SQL CREATE TABLE `city` (`id` VARCHAR(16) NOT NULL, `version` BIGINT NOT NULL, `date_created` DATETIME NOT NULL, `last_updated` DATETIME NOT NULL, `name` VARCHAR(255) NOT NULL, CONSTRAINT `cityPK` PRIMARY KEY (`id`)) type=InnoDB: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 1
    Line | Method
->>   62 | execute           in liquibase.executor.jvm.JdbcExecutor
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    104 | execute           in     ''
|   1091 | execute . . . . . in liquibase.database.AbstractDatabase
|   1075 | executeStatements in     ''
|    317 | execute . . . . . in liquibase.changelog.ChangeSet
|     27 | visit             in liquibase.changelog.visitor.UpdateVisitor
|     58 | run . . . . . . . in liquibase.changelog.ChangeLogIterator
|    114 | update            in liquibase.Liquibase
|     26 | doCall . . . . .  in DbmUpdate$_run_closure1_closure2
|     59 | doCall            in _DatabaseMigrationCommon_groovy$_run_closure2_closure11
|    133 | executeInSession  in grails.plugin.databasemigration.MigrationUtils
|     51 | doCall            in _DatabaseMigrationCommon_groovy$_run_closure2
^     25 | doCall . . . . .  in DbmUpdate$_run_closure1
Caused by MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 1
->>  400 | handleNewInstance in com.mysql.jdbc.Util
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    383 | getInstance       in     ''
|    980 | createSQLException in com.mysql.jdbc.SQLError
|   3847 | checkErrorPacket  in com.mysql.jdbc.MysqlIO
|   3783 | checkErrorPacket  in     ''
|   2447 | sendCommand       in     ''
|   2594 | sqlQueryDirect .  in     ''
|   2541 | execSQL           in com.mysql.jdbc.ConnectionImpl
|   2499 | execSQL . . . . . in     ''
|    844 | execute           in com.mysql.jdbc.StatementImpl
|    748 | execute . . . . . in     ''
|     92 | doInStatement     in liquibase.executor.jvm.JdbcExecutor$1ExecuteStatementCallback
|     55 | execute . . . . . in liquibase.executor.jvm.JdbcExecutor
|    104 | execute           in     ''
|   1091 | execute . . . . . in liquibase.database.AbstractDatabase
|   1075 | executeStatements in     ''
|    317 | execute . . . . . in liquibase.changelog.ChangeSet
|     27 | visit             in liquibase.changelog.visitor.UpdateVisitor
|     58 | run . . . . . . . in liquibase.changelog.ChangeLogIterator
|    114 | update            in liquibase.Liquibase
|     26 | doCall . . . . .  in DbmUpdate$_run_closure1_closure2
|     59 | doCall            in _DatabaseMigrationCommon_groovy$_run_closure2_closure11
|    133 | executeInSession  in grails.plugin.databasemigration.MigrationUtils
|     51 | doCall            in _DatabaseMigrationCommon_groovy$_run_closure2
^     25 | doCall . . . . .  in DbmUpdate$_run_closure1
liquibase.exception.MigrationFailedException: Migration failed for change set Update4.groovy::1437215773652-1::mg (generated):
     Reason: liquibase.exception.DatabaseException: Error executing SQL CREATE TABLE `city` (`id` VARCHAR(16) NOT NULL, `version` BIGINT NOT NULL, `date_created` DATETIME NOT NULL, `last_updated` DATETIME NOT NULL, `name` VARCHAR(255) NOT NULL, CONSTRAINT `cityPK` PRIMARY KEY (`id`)) type=InnoDB: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 1:
          Caused By: Error executing SQL CREATE TABLE `city` (`id` VARCHAR(16) NOT NULL, `version` BIGINT NOT NULL, `date_created` DATETIME NOT NULL, `last_updated` DATETIME NOT NULL, `name` VARCHAR(255) NOT NULL, CONSTRAINT `cityPK` PRIMARY KEY (`id`)) type=InnoDB: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 1:
          Caused By: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 1
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:347)
    at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:27)
    at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:58)
    at liquibase.Liquibase.update(Liquibase.java:114)
    at DbmUpdate$_run_closure1_closure2.doCall(DbmUpdate:26)
    at _DatabaseMigrationCommon_groovy$_run_closure2_closure11.doCall(_DatabaseMigrationCommon_groovy:59)
    at grails.plugin.databasemigration.MigrationUtils.executeInSession(MigrationUtils.groovy:133)
    at _DatabaseMigrationCommon_groovy$_run_closure2.doCall(_DatabaseMigrationCommon_groovy:51)
    at DbmUpdate$_run_closure1.doCall(DbmUpdate:25)
Caused by: liquibase.exception.DatabaseException: Error executing SQL CREATE TABLE `city` (`id` VARCHAR(16) NOT NULL, `version` BIGINT NOT NULL, `date_created` DATETIME NOT NULL, `last_updated` DATETIME NOT NULL, `name` VARCHAR(255) NOT NULL, CONSTRAINT `cityPK` PRIMARY KEY (`id`)) type=InnoDB: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 1
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:62)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:104)
    at liquibase.database.AbstractDatabase.execute(AbstractDatabase.java:1091)
    at liquibase.database.AbstractDatabase.executeStatements(AbstractDatabase.java:1075)
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:317)
    ... 8 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 1
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:400)
    at com.mysql.jdbc.Util.getInstance(Util.java:383)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:980)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2541)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2499)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:844)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:748)
    at liquibase.executor.jvm.JdbcExecutor$1ExecuteStatementCallback.doInStatement(JdbcExecutor.java:92)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55)
    ... 12 more

尝试上述代码。或参见演示:Demo

答案 1 :(得分:0)

即使在文本框设置之前,您也可以获得文本框的价值。您应该尝试从事件参数中获取值。您可以使用event.keycode获取按键,如下所示

$(function() {
      $("#name").keydown(function(event){
            // Just to make sure it works in all browsers
            var keycode = (event.keyCode ? event.keyCode : event.which);
            var userInput = String.fromCharCode(keycode);
             alert("name"+userInput );
      });
  });
相关问题