禁用HTML SELECT标记中的键盘 [英] Disable keyboard in HTML SELECT tag

查看:120
本文介绍了禁用HTML SELECT标记中的键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想禁用HTML SELECT标签的键盘,以便用户只能使用鼠标选择选项。

I want to disable the keyboard for an HTML SELECT tag so the user can only use a mouse to select the options.

我试过 event.cancelBubble = true onkeydown onkeyup onkeypress 没有运气的事件。

I've tried event.cancelBubble=true on the onkeydown, onkeyup and onkeypress events with no luck.

任何想法?

推荐答案

有人给我留下了正确的答案然后因某种原因将其删除了,所以这里是:

Someone left me the correct answer and then removed it for some reason, so here it is:

function IgnoreAlpha(e) {
  if (!e) {
    e = window.event;
  }
  if (e.keyCode >= 65 && e.keyCode <= 90) // A to Z
  {
    e.returnValue = false;
    e.cancel = true;
  }
}

<p>
  <select id="MySelect" name="MySelect" onkeydown="IgnoreAlpha(event);">
    <option selected="selected " value=" " />
    <option value="A ">A</option>
    <option value="B ">B</option>
    <option value="C ">C</option>
  </select>
</p>

这篇关于禁用HTML SELECT标记中的键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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