旧行李 2014-08-24 20:59 采纳率: 25%
浏览 21

为什么提交时会触发事件?

在注册中,我想让用户点击进入后电子邮件就自动消失。我已经添加了JS来隐藏和取消隐藏占位符文本。

代码:

<form id="form" action="https://xxxx.com/subscribe/post-json?u=xxx&id=xx&c=?" method="GET">
  <input type="hidden" name="u" value="xxx">
  <input type="hidden" name="id" value="xxx">
  <input id="email" type="EMAIL" autocapitalize="off" autocorrect="off" name="MERGE0" id="MERGE0" size="25" placeholder= "Type your email and press enter">
  <p id="response"></p>
</form>

JS代码:

<script >

 var text = document.getElementById("email");  

    text.onfocus = function() {
        if ( text.placeholder == "Type your email and press enter") {
            text.placeholder = "";
        }      
    };    
    text.onblur = function() {
        if ( text.placeholder == "") {
            text.placeholder = "Type your email and press enter";
        }
    };
</script>

我试图创建一个函数来触发该事件,但它不起作用:

 function checkEnter(event)
{
   if (event.keyCode == 13) {text.placeholder = "cool";}
};

你们能发现我的代码出了什么问题吗?

  • 写回答

2条回答 默认 最新

  • weixin_33726313 2014-08-24 21:24
    关注

    You need to add a event listener for the enter key. You could remove your function checkEnter and use this instead:

    document.querySelector('#email').addEventListener('keypress', function (e) {
      var key = e.which || e.keyCode;
      if (key == 13) {
        text.placeholder = "cool";
      }
    };
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题