按下Enter键后清除文本字段 [英] Clear text field after keypress enter

查看:72
本文介绍了按下Enter键后清除文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是简单的聊天脚本,需要按Enter键后清除文本字段的方法.

I'm using a simple chat script and need a way to clear the text field after pressing enter.

我可以通过 onfocus 清除鼠标单击上的文本字段,但是按键使我难以理解.还有一种方法可以用一个命令来使代码更整洁吗?

I got it to clear text field on mouse click with onfocus but keypress eludes me. Also is there a way to do both with one command to make the code cleaner?

<form onsubmit="chat.sendMsg(); return false;">
    <input onfocus="if(this.value !== '') {this.value=''}" onblur="if(this.value == '')" type="text" id="msg" name="msg" autofocus="true" placeholder="Type Your Meassage Here" />
    <input type="submit" value="Enter" />
</form>

推荐答案

<form onsubmit="chat.sendMsg(); this.msg.value = ''; return false;">
    <input onfocus="if(this.value !== '') {this.value=''}" onblur="if(this.value == '')" type="text" id="msg" name="msg" autofocus="true" placeholder="Type Your Meassage Here" />
    <input type="submit" value="Enter" />
</form> 

<form onsubmit="chat.sendMsg(); this[0].value = ''; return false;">
        <input onfocus="if(this.value !== '') {this.value=''}" onblur="if(this.value == '')" type="text" id="msg" name="msg" autofocus="true" placeholder="Type Your Meassage Here" />
        <input type="submit" value="Enter" />
</form>

这篇关于按下Enter键后清除文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆