【问题标题】:Is it possible to remove an input's content when it's focused by pressing a key? [closed]当通过按键获得焦点时是否可以删除输入的内容? [关闭]
【发布时间】:2013-07-26 00:20:25
【问题描述】:

是否可以在通过按键获得焦点时删除输入的内容?

谢谢你的期待! :)

【问题讨论】:

  • 你知道,我敢打赌可能的;你为什么不尝试一下,然后有任何问题回来?我们喜欢帮助那些自助的人。
  • 我不是说按退格键...

标签: javascript jquery input


【解决方案1】:

是的。使用 jQuery,您可以执行以下操作(请参阅 http://jsfiddle.net/5tsNW/1/ 以获取演示):

HTML:

<input type="text" id="testInp" />

JavaScript:

$(function() {
    $("#testInp").on("keydown", function (e) {
        // backspace is key code 8
        if(e.keyCode === 8) {
            $(this).val("");
        }
    });
});

【讨论】:

  • 天哪,我尝试了几次,但现在可以了:o 谢谢,无论如何:D
【解决方案2】:

试试这个:

$('input').keypress(function(event) {
    if ( event.which == 8 ) { // backspace
        $(this).val('');
    }
}

【讨论】:

    猜你喜欢
    • 2019-08-26
    • 2015-03-29
    • 2013-07-26
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多