如何触发ESC按钮以清除在文本框中输入的文本?

时间:2010-09-01 07:07:32

标签: javascript html css

   <input type="text" id="search" size="25" autocomplete="off"/>

我知道这是

onkeydown="if (event.keyCode == 27)

4 个答案:

答案 0 :(得分:8)

声明按下某个键时将调用的函数:

function onkeypressed(evt, input) {
    var code = evt.charCode || evt.keyCode;
    if (code == 27) {
        input.value = '';
    }
}

以及相应的标记:

<input type="text" id="search" size="25" autocomplete="off" 
       onkeydown="onkeypressed(event, this);" />

答案 1 :(得分:5)

<input type="text" value="" onkeyup="if ( event.keyCode == 27 ) this.value=''" />

这应该有用。

答案 2 :(得分:1)

function keyPressed(evt) {
 if (evt.keyCode == 27) {
    //clear your textbox content here...
    document.getElementById("search").value = '';
 }
}

然后在你的输入标签中......

<input type="text" onkeypress="keyPressed(event)" id="search" ...>

答案 3 :(得分:0)

&#13;
&#13;
$('input[type=text]').each(function (e) {
    $(this).keyup(function (evt) {
        var code = evt.charCode || evt.keyCode;
        if (code == 27) {
            $(this).val('');
        }
    })
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" autofocus value="input data" placeholder="ESC button clear" style="padding:5px;">
<p>Hit esc button to see result</p>
&#13;
&#13;
&#13;

相关问题