一个不复杂的菜单问题等了一个月没等到答案

colee 2002-11-30 01:07:13
现在有一个选择菜单<select..></select>,里面有很多选择值,要选到想找的值很难,有什么方法可以快点选到想选的值呢?
(我想这样:鼠标先点到选择菜单,然后键盘敲入想选的字符<或它的前面几个字符,就会逐渐出现 含有敲入字符的值>,但是我不能实现 )

在Asp专栏中一个月了都没人帮我解决,希望这里的高手们帮帮手
...全文
61 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiushuiwuhen 2002-12-04
  • 打赏
  • 举报
回复
<select name="username" onkeydown=spellList() onchange=alert(this[selectedIndex].sp)>
<option>H102452PLFW</option>
<option>H102453PLFW</option>
<option>H102352PLFW</option>
<option>/102354PLFW</option>
<option>h202154PLFW</option>
<option>-102354PLFW</option>
</select>

<script>
var sel="",timer=null,re="";
function spellList(){
/********(qiushuiwuhen 2002-9-20)***********/
with(window.event){
with(srcElement){
if(keyCode<48&&keyCode!=8)return;
if(keyCode>95&&keyCode<106)keyCode-=48
if(keyCode==8)sel=sel.slice(0,-1)
else if(keyCode==189||keyCode==109)sel+="-"
else if(keyCode==191)sel+="/"
//根据自己的需要,增加特殊符号的处理
else sel+=String.fromCharCode(keyCode)
window.status=sel
for(i=0;i<length;i++){
if(options[i].text.toLowerCase().indexOf(sel.toLowerCase())==0){selectedIndex=i;break;}
}
}
returnValue=false;
clearTimeout(timer)
timer=setTimeout("sel=''",2000);//延长时间
}
}
</script>

colee 2002-12-03
  • 打赏
  • 举报
回复
TO freefalcon(心宇):
为了增加对符号“/”的处理,
我加了 else if(keyCode==191)sel+="/",但是无效

为什么对符号“-”的处理有两个keycode值?
即:else if(keyCode==189||keyCode==109)sel+="-"

顺便请帮忙给出对符号“/”的处理应该怎么写,
就此结束
freefalcon 2002-12-03
  • 打赏
  • 举报
回复
编写如下的一个html页面
<body onkeydown="window.status=(event.keyCode)">
你便可以在状态栏中看见键码了
colee 2002-12-03
  • 打赏
  • 举报
回复
请恕我对Javascript的陌生
我不知道怎么“//根据自己的需要,增加特殊符号的处理”
那些常用字符的keycode是多少,比如“/”的

应该加一个怎样的else if。。。
colee 2002-12-02
  • 打赏
  • 举报
回复
junguo(junguo) :我把timelimit改大了,但结果反而选不到了,改得小点(我甚至把它设为零)也没什么变化,你程式里面:timer好像等于lasttimer?

秋水兄,你的那个程式只对大写字母和数字有效,可不可以不分大小写,而且对减号等几个非常常用的字符也有效?
另外可不可以控制缓冲时间?
junguo 2002-12-02
  • 打赏
  • 举报
回复
var timelimit=2000;
把这个值改大就好了
qiushuiwuhen 2002-12-02
  • 打赏
  • 举报
回复
同理可做呀,增加了退格键backspace的功能,如果输错,可以退格

<select name="username" onkeydown=spellList() onchange=alert(this[selectedIndex].sp)>
<option>H102452PLFW</option>
<option>H102453PLFW</option>
<option>H102352PLFW</option>
<option>H102354PLFW</option>
</select>

<script>
var sel="",timer=null;
function spellList(){
/********(qiushuiwuhen 2002-9-20)***********/
with(window.event){
with(srcElement){
if(keyCode<48&&keyCode!=8)return;
if(keyCode>95)keyCode-=48
if(keyCode==8)sel=sel.slice(0,-1)
else sel+=String.fromCharCode(keyCode)
window.status=sel
for(i=0;i<length;i++){
if(options[i].text.indexOf(sel)==0){selectedIndex=i;break;}
}
}
returnValue=false;
clearTimeout(timer)
timer=setTimeout("sel=''",1000);
}
}
</script>
colee 2002-12-02
  • 打赏
  • 举报
回复
qiushuiwuhen(秋水无恨) 的这个程式(在ASP专栏里已经回复我了)只对汉字有用。(不过这种思想真的很好,可不可以给我个支持 ‘选择值里只含字母或数字等其他常用字符’ 的脚本)

junguo(junguo)的程式我有两点疑问:
1、必须得非常连续快速正确地输入要选择的值,稍慢一点或不连续都不行。怎么来延长缓冲时间呢???
2、假设选择菜单里有个值为:"H102452PLFW",现在要选择它,若敲错了其中一个字符,则会转到这个以这个敲错了的字符为首的值中去,所以必须重新再敲入。

请再关注帮忙,谢谢
bencalie 2002-12-02
  • 打赏
  • 举报
回复
微软的例子,在select获得焦点的情况下,按相应键跳转到相应字母开始的第一个选项

<HTML>
<HEAD>

<SCRIPT language=javascript>
// Auto-select listbox

// mike pope, Visual Basic UE

// This script and the listbox on this page illustrates one
// way to create an "auto-complete" listbox, where the

var toFind = ""; // Variable that acts as keyboard buffer
var timeoutID = ""; // Process id for timer (used when stopping
// the timeout)
timeoutInterval = 250; // Milliseconds. Shorten to cause keyboard
// buffer to be cleared faster
var timeoutCtr = 0; // Initialization of timer count down
var timeoutCtrLimit = 3 ; // Number of times to allow timer to count
// down
var oControl = ""; // Maintains a global reference to the
// control that the user is working with.

function listbox_onkeypress(){
// This function is called when the user presses a key while focus is in
// the listbox. It maintains the keyboard buffer.
// Each time the user presses a key, the timer is restarted.
// First, stop the previous timer; this function will restart it.
window.clearInterval(timeoutID)

// Which control raised the event? We'll need to know which control to
// set the selection in.
oControl = window.event.srcElement;

var keycode = window.event.keyCode;
if(keycode >= 32 ){
// What character did the user type?
var c = String.fromCharCode(keycode);
c = c.toUpperCase();
// Convert it to uppercase so that comparisons don't fail
toFind += c ; // Add to the keyboard buffer
find(); // Search the listbox
timeoutID = window.setInterval("idle()", timeoutInterval);
// Restart the timer
}
}

function listbox_onblur(){
// This function is called when the user leaves the listbox.

window.clearInterval(timeoutID);
resetToFind();
}

function idle(){
// This function is called if the timeout expires. If this is the
// third (by default) time that the idle function has been called,
// it stops the timer and clears the keyboard buffer

timeoutCtr += 1
if(timeoutCtr > timeoutCtrLimit){
resetToFind();
timeoutCtr = 0;
window.clearInterval(timeoutID);
}
}

function resetToFind(){
toFind = ""
}


function find(){
// Walk through the select list looking for a match

var allOptions = document.all.item(oControl.id);

for (i=0; i < allOptions.length; i++){
// Gets the next item from the listbox
nextOptionText = allOptions(i).text.toUpperCase();

// By default, the values in the listbox and as entered by the
// user are strings. This causes a string comparison to be made,
// which is not correct for numbers (1 < 11 < 2).
// The following lines coerce numbers into an (internal) number
// format so that the subsequent comparison is done as a
// number (1 < 2 < 11).

if(!isNaN(nextOptionText) && !isNaN(toFind) ){
nextOptionText *= 1; // coerce into number
toFind *= 1;
}

// Does the next item match exactly what the user typed?
if(toFind == nextOptionText){
// OK, we can stop at this option. Set focus here
oControl.selectedIndex = i;
window.event.returnValue = false;
break;
}

// If the string does not match exactly, find which two entries
// it should be between.
if(i < allOptions.length-1){

// If we are not yet at the last listbox item, see if the
// search string comes between the current entry and the next
// one. If so, place the selection there.

lookAheadOptionText = allOptions(i+1).text.toUpperCase() ;
if( (toFind > nextOptionText) &&
(toFind < lookAheadOptionText) ){
oControl.selectedIndex = i+1;
window.event.cancelBubble = true;
window.event.returnValue = false;
break;
} // if
} // if

else{

// If we are at the end of the entries and the search string
// is still higher than the entries, select the last entry

if(toFind > nextOptionText){
oControl.selectedIndex = allOptions.length-1 // stick it
// at the end
window.event.cancelBubble = true;
window.event.returnValue = false;
break;
} // if
} // else
} // for
} // function
</SCRIPT>
</HEAD>
<BODY>
<P><B>Text</B></P>
<SELECT ID=select1 tabindex=1 width=20 size=7 style="width:125px"
onkeypress="listbox_onkeypress()" onblur="listbox_onblur()">
<OPTION value=2>Abby</OPTION>
<OPTION value=1>Alice</OPTION>
<OPTION value=3>Barry</OPTION>
<OPTION value=3>Beth</OPTION>
<OPTION value=3>Bobby</OPTION>
<OPTION value=3>Catherine</OPTION>
<OPTION value=3>Chuck</OPTION>
<OPTION value=3>Dave</OPTION>
<OPTION value=3>Dick</OPTION>
<OPTION value=3>Dino</OPTION>
<OPTION value=3>Elmer</OPTION>
<OPTION value=3>Emily</OPTION>
<OPTION value=3>Ethel</OPTION>
<OPTION value=3>William</OPTION>
<OPTION value=3>Xerxes</OPTION>
<OPTION value=3>Yolanda</OPTION>
<OPTION value=3>Zachary</OPTION>
</SELECT>
</BODY>
</HTML>
freefalcon 2002-12-02
  • 打赏
  • 举报
回复
<select name="username" onkeydown=spellList() onchange=alert(this[selectedIndex].sp)>
<option>H102452PLFW</option>
<option>H102453PLFW</option>
<option>H102352PLFW</option>
<option>H102354PLFW</option>
<option>h202154PLFW</option>
<option>-102354PLFW</option>
</select>

<script>
var sel="",timer=null,re="";
function spellList(){
/********(qiushuiwuhen 2002-9-20)***********/
with(window.event){
with(srcElement){
if(keyCode<48&&keyCode!=8)return;
if(keyCode>95&&keyCode<106)keyCode-=48
if(keyCode==8)sel=sel.slice(0,-1)
else if(keyCode==189||keyCode==109)sel+="-"
//根据自己的需要,增加特殊符号的处理
else sel+=String.fromCharCode(keyCode)
window.status=sel
re=eval("/"+sel+"/i")
for(i=0;i<length;i++){
if(options[i].text.search(re)==0){selectedIndex=i;break;}
}
}
returnValue=false;
clearTimeout(timer)
timer=setTimeout("sel=''",2000);//延长时间
}
}
</script>
qiushuiwuhen 2002-11-30
  • 打赏
  • 举报
回复
输入名字的首字缩写即可找到,如张三火(zsh)<select name="username" onkeydown=spellList() onchange=alert(this[selectedIndex].sp)>
<option value="ZS" >张三</option>
<option value="ZSH">张三火</option>
<option value="LS">李四</option>
<option value="LSS">李四水</option>
</select>

<script>
var sel="",timer=null;
function spellList(){
/********(qiushuiwuhen 2002-9-20)***********/
with(window.event){
with(srcElement){
if(keyCode<48)return;
if(keyCode>95)keyCode-=48
sel+=String.fromCharCode(keyCode)
window.status=sel
for(i=0;i<length;i++){
if(options[i].value.indexOf(sel)==0){selectedIndex=i;break;}
}
}
returnValue=false;
clearTimeout(timer)
timer=setTimeout("sel=''",500);
}
}
</script>
junguo 2002-11-30
  • 打赏
  • 举报
回复
var strKey;
var lasttimer=Date.parse(Date());
var timelimit=2000;
function onKeyPress()
{
if(strKey==null)
{
strKey="";
}
var length;
var strText;
var timer;
var TheForm;
TheForm=event.srcElement;
timer=Date.parse(Date());
if(timer-lasttimer>timelimit)
{strKey="";}
lasttimer=timer;
strKey=strKey + String.fromCharCode(window.event.keyCode);
length=TheForm.length;
for(i=0;i<length;i++)
{
strText=TheForm.options[i].text;
strText=strText.toLowerCase();
intI=strText.indexOf(strKey);
if (intI==0)
{
TheForm.options[i].selected=true;
window.event.returnValue=false;
return;
}
}
}
网上很多地方可以找到本书的下载,我唯一能够额外贡献的是,提供多格式的打包下载,包括:PDF格式,离线网页格式,在线网页格式。 如果您觉得下载比较麻烦,那么建议您直接使用在线教程,网址是:http://learn.akae.cn/media/index.html 如果您只需要一个可以打印的PDF格式文件,那么建议你下载Defonds的资源,因为我的PDF格式文件就是从他那下的,资源链接是:http://download.csdn.net/detail/defonds/2855361 如果您和我一样,希望在多种环境下都可以方便地使用本书,那么欢迎您下载我提供的这个打包资源! 对本书的描述 版权 © 2008, 2009 宋劲杉, 北京亚嵌教育研究中心 这本书最初是为北京亚嵌教育研究中心的嵌入式Linux系统工程师就业班课程量身定做的教材之一,作者宋劲松。 这是一本从零基础开始学习编程的书,不要求读者有任何编程经验,但读者应勤于思考。本书尽最大努力理清概念之间的依赖关系,力求一站式学习,读者不需要为了找一个概念的定义去翻其它书,也不需要为了搞清楚一个概念在本书中前后一通乱翻,只需从前到后按顺序学习即可。但一站式学习并不等于傻瓜式学习,有些章节有一定的难度,需要积极思考才能领会。本书可以替你节省时间,但不能替你思考,不要指望像看小说一样走马观花看一遍就能学会。 本书不是孤立地讲C语言,而是和编译原理、操作系统、计算机体系结构结合起来讲。或者说,本书的内容只是以C语言为载体,真正讲的是计算机的原理和程序的原理。 强调基本概念和基本原理,在编排顺序上非常重视概念之间的依赖关系,每次引入一个新的概念,只依赖于前面章节已经讲过的概念,而绝不会依赖后面章节要讲的概念。有些地方为了叙述得完整,也会引用后面要讲的内容,比如说“有关XX我们到XX章再仔细讲解”,凡是这种引用都不是必要的依赖,可以当它不存在,只管继续往下看就行了。 这本书定位在入门级,虽然内容很多,但不是一本百科全书,除了C语言基本要讲透之外其它内容都不深入,书中列出了很多参考资料,是读者进一步学习的起点。K&R的第一章是一个Whirlwind Tour,把全书的内容简单过了一遍,然后再逐个深入进去讲解。本书也可以看作是计算机专业课程体系的一个Whirlwind Tour,学习完本书之后有了一个全局观,再去学习那些参考资料就应该很容易上手了。 本书的主要内容包括三大部分: C语言入门。介绍基本的C语法,帮助有任何编程经验的读者理解什么是程序,怎么写程序,培养程序员的思维习惯,找到编程的感觉。前半部分改编自《How To Think Like A Computer Scientist: Learning with C++》(Allen B. Downey.) C语言本质。结合计算机和操作系统的原理讲解C程序是怎么编译、链接、运行的,同时全面介绍C的语法。位运算的章节改编自亚嵌教育林小竹老师的讲义,链表和二叉树的章节改编自亚嵌教育朱老师的讲义。汇编语言的章节改编自《Programming from the Ground Up: An Introduction to Programming using Linux Assembly Language》(Jonathan Bartlett.)在该书的最后一章提到,学习编程有两种Approach,一种是Bottom Up,一种是Top Down,各有优缺点,需要两者结合起来。所以作者编这本书的思路是,第一部分Top Down,第二部分Bottom Up,第三部分可以算填了中间的空隙,三部分全都围绕C语言展开。 Linux系统编程。介绍各种Linux系统函数和内核的工作原理。Socket编程的章节改编自亚嵌教育卫剑钒老师的讲义。 为什么要在Linux平台上学C语言?用Windows学C语言不好吗? 用Windows还真的是学不好C语言。C语言是一种面向底层的编程语言,要写好C程序,必须对操作系统的工作原理非常清楚,因为操作系统也是用C写的,我们用C写应用程序直接使用操作系统提供的接口。既然你选择了看这本书,你一定了解:Linux是一种开源的操作系统,你有任何疑问都可以从源代码和文档中找到答案,即使你看不懂源代码,也找不到文档,也很容易找个高手教你,各种邮件列表、新闻组和论坛上从来都不缺乐于助人的高手;而Windows是一种封闭的操作系统,除了微软的员工别人都看不到它的源代码,只能通过文档去猜测它的工作原理,更糟糕的是,微软向来喜欢藏着揶着,好用的功能留着自己用,而不会写到文档里公开。本书的第一部分在Linux或Windows平台上学习都可以,但第二部分和第三部分介绍了很多Linux操作系统的原理以帮助读者更深入地理解C语言,只能在Linux平台上学习。 Windows平台上的开发工具往往和各种集成开发环境(IDE,Integrated Development Environment)绑在一起,例如Visual Studio、Eclipse等。使用IDE确实很便捷,但IDE对于初学者绝对不是好东西。微软喜欢宣扬傻瓜式编程的理念,告诉你用鼠标拖几个控件,然后点一个按钮就可以编译出程序来,但是真正有用的程序有哪个是这么拖出来的?很多从Windows平台入门学编程的人,编了好几年程序,还是只知道编完程序点一个按钮就可以跑了,把几个源文件拖到一个项目里就可以编译到一起了,如果有更复杂的需求他们就傻眼了,因为他们脑子里只有按钮、菜单的概念,根本有编译器、链接器、Makefile的概念,甚至连命令行都用过,然而这些都是初学编程就应该建立起来的基本概念。另一方面,编译器、链接器和C语言的语法有密切的关系,不了解编译器、链接器的工作原理,也不可能真正掌握C的语法。所以,IDE并有帮助你学习,而是阻碍了你学习,本来要学好C编程只要把语法和编译命令学会就行了,现在有了IDE,除了学会语法和编译命令,你还得弄清楚编译命令和IDE是怎么集成的,这才算学明白了,本来就很复杂的学习任务被IDE搞得更加复杂了。Linux用户的使用习惯从来都是以敲命令为主,以鼠标操作为辅,从学编程的第一天起就要敲命令编译程序,等到你把这些基本概念都搞清楚了,你觉得哪个IDE好用你再去用,不过到那时候你可能会更喜欢vi或emacs而不是IDE了。

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧