输入按键执行按钮单击 [英] Enter key press execute button click

查看:56
本文介绍了输入按键执行按钮单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

填写文本框字段后输入按钮时用于执行提交按钮的javascript。但它不能正常工作......我的错误是什么......请帮助我找到它。

The javascript used for executing the submit button when enter button press after filling the textbox field. but it is not working properly...What will be my mistake...plz help me to find it.

<asp:TextBox ID="txtComment" runat="server" MaxLength="300"   onKeyPress="return processKey(event) ">
<script type="text/javascript">
      function processKey(e) {
      var key;
      if (window.event)
          key = window.event.keyCode;
      else
          key = e.which;

      if (key == 13) {

          var btn = document.getElementById('ctl00_btnsend');

          if (btn != null) {

              btn.click();

              event.keyCode = 0
          }
      }
}
</script>

推荐答案

像这样调用js函数onBlur =functionName(event)你也可以使用onChange =functionName
Call js function like this onBlur="functionName(event)" you can also use onChange="functionName"


您的代码似乎没问题。您是否在文本框中添加了以下属性?

Your code seems fine. Have you added following attributes to your textbox?
txtbox1.Attributes.Add("onKeyPress", 
                   "processKey('" + btnSearch.ClientID + "',event)"); //txtbox1 is an example, rename it your textbox name.







另外看看这个链接,javascript中的相同功能已被写成你正在使用的那个:



捕获输入密钥以使按钮单击 [ ^ ]



尝试此链接:

using- javascript-detect-enter-key-pressed-event-in-a-textbox-and-perform-the-action / [ ^ ]


这里使用简单的Jquery代码...



Here simple Jquery code to do the same...

<script type="text/javascript">


这篇关于输入按键执行按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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