【问题标题】:jquery detect enter key on dynamically created textboxjquery在动态创建的文本框中检测回车键
【发布时间】:2015-02-21 03:09:48
【问题描述】:

我有以下创建文本框的代码:

$(".vocab-list").append( $('<li style="margin-left: 307px;" class="vocab-word" id="vocab_' + vocabWords[wordId]['id']+ '"><img width="230px" height="230px" src="' + vocabWords[wordId]['imageURL'] + '" /><div><input type="text" spellcheck="false" id="writeWord" autocorrect="off" maxlength="200" style="width: 230px;border: 0;border-bottom: 1px solid #C8C6C6;border-radius: 0;color:#6e572f;font-size:24px;font-weight:500;"></div></li>').hide().fadeIn(600));

如何检测回车键?以下无效

$('#writeWord').bind("enterKey",function(e){
alert("Enter");
});

【问题讨论】:

  • Javascript capture key的可能重复
  • writeWord 不是标准的 HTML 标签,你的意思是 .writeWord?创建文本框时看不到 .writeWord 元素
  • 我的意思是#writeWord ...我只需要从动态创建的文本框中检测回车键
  • 您可能对“映射 [Enter] 键以像 [Tab] 键一样工作”stackoverflow.com/a/27545387/1500195 感兴趣

标签: jquery


【解决方案1】:

.on 用于动态HTML - 检测keyup 事件,然后检测向上键是否为回车键:

$(".vocab-list").on("keyup", "#writeWord", function(e) {
    if (e.which == 13) {
        alert("Enter");
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2012-08-12
    相关资源
    最近更新 更多