回车键未隐藏 [英] Return key not being suppressed

查看:130
本文介绍了回车键未隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义gridview,具有可导出到excel功能的功能,在每一行中都有一列用于编辑/删除按钮的列,以及用于添加新项目的页脚.

像这样的东西:


布朗科
编辑/删除
FLTPLAN
编辑/删除
LASSO
编辑/删除
PINTO
编辑/删除
牧场
编辑/删除
RODEO
编辑/删除
德克萨斯州
编辑/删除
测试
编辑/删除
|空文本框|
添加

(对不起,我的HTML很懒)

导航到该页面后,用户可以按回车键,并且由于它是页面上的唯一按钮,因此将导出到excel按钮.我使用以下Javascript停止了此操作:

I have a custom gridview with an export to excel feature, a column for edit/delete buttons in each row that is populated, and a footer that allows for new items to be added.

Somthing a little like this:


Showing 1-8 of 8
Results per page: 25 50 All |Export Button|
BRONCO
Edit / Delete
FLTPLAN
Edit / Delete
LASSO
Edit / Delete
PINTO
Edit / Delete
RANCH
Edit / Delete
RODEO
Edit / Delete
TEXAN
Edit / Delete
TEST
Edit / Delete
|Empty Text Box|
Add

(excuse my lazy html)

After navigating to that page a user would be able press the return key and, because it is the only button on the page, the export to excel button would be activated. I used the following Javascript to stop this:

<script type="text/javascript">
        function stopRKey(evt) { 
                  var evt = (evt) ? evt : ((event) ? event : null); 
                  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
                  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;} 
                } 
                document.onkeypress = stopRKey;
            
</script>


尽管这对于用户导航到页面非常有用,但是在gridview中编辑项目后,用户可以按Enter并再次获取导出按钮.我知道脚本可以正常工作,所以我感到非常困惑,为什么在我在gridview中编辑/添加某些内容后它仍然允许返回键.

其他(可能)值得注意的物品:
gridview在更新面板中.将javascript放到udp中不会改变任何内容.
gridview正在使用linq数据源.
我曾尝试在.aspx页和母版页中同时使用jscript(由于某种不合逻辑的原因,它们都同时使用).


While this is great for when the user navigates to the page, after editing an item in the gridview, the user can press enter and get the export button again. I know the script is working so I am absolutely perplexed why it''s still allowing the return key after I edit/add something in the gridview.

Other (possibly) noteworthy items:
The gridview is in an update panel. Putting the javascript in the udp doesn''t change anything.
The gridview is using a linq data source.
I''ve tried using the jscript in both the .aspx page and the master page (and both at the same time for some illogical reason).

推荐答案

原因这是因为事件使文档节点树冒泡,其中document在根级别,而树的子节点和叶子是您的HTML元素.仅当在树的任何子节点上未安装处理程序时,事件才会在树上冒泡.如果导航到该页面,并且没有任何具有该事件处理程序的元素具有焦点,则该事件最终将上升到根
The cause for this is that events bubble up the document node tree with document being at the root level and the sub nodes and leaves of that tree being your HTML elements. The event will only bubble up the tree when there are no handler installed at any sub nodes of said tree. If you navigate to that page and no element having a handler for that event has the focus the event will eventually rise up to the root
document

元素,并由您的脚本处理.如果您想阻止此事件触发导出按钮,则必须在我要说的按钮上安装该处理程序.

最好的问候,

-MRB

element and be handled by you script. If you want to supress this event triggering the export button you''d have to install that handler on said button I''d say.

Best Regards,

-MRB


这篇关于回车键未隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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