使用JS搜索下拉框。 (代码示例) [英] Searching a drop-down box with JS. (code sample)

查看:63
本文介绍了使用JS搜索下拉框。 (代码示例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天把它放在一起,我想我会分享它。

它适用于IE和Mozilla Firefox。我发布了类似于这个月的东西

,但它更长了,只有

在IE中工作。


Shawn


函数searchList(inText,drpBox){


var iCount;

var regEx = new RegExp(inText ,''我');


for(iCount = 0; iCount< drpBox.length; iCount ++){

if(drpBox [iCount]。 value.match(regEx)){

drpBox.selectedIndex = iCount;

休息;

}

}


}


把它放在下拉框附近的表格上:


< input type =" text"名称= QUOT; txtSearch" ID = QUOT; txtSearch" value =" search list"

onfocus =" if(this.value ==''search list''){this.value ='''';}"

onkeyup =" searchList(this.value,document.forms [''frmAdd'']。drpSponsor);"

/>< br />

I put this together yesterday, and I thought I''d share it.
It works in both IE and Mozilla Firefox. I posted something
similar to this months back, but it was much longer, and only
worked in IE.

Shawn

function searchList(inText, drpBox){

var iCount;
var regEx = new RegExp(inText, ''i'');

for (iCount=0; iCount<drpBox.length; iCount++){
if (drpBox[iCount].value.match(regEx)){
drpBox.selectedIndex = iCount;
break;
}
}

}


Put this on the form near the drop-down box:

<input type="text" name="txtSearch" id="txtSearch" value="search list"
onfocus="if (this.value == ''search list''){ this.value='''';}"
onkeyup="searchList(this.value, document.forms[''frmAdd''].drpSponsor);"
/><br/>

推荐答案



" Shawn Milo" <是ne ******** @ linurati.net>在留言中写道

news:c2 ************************** @ posting.google.c om ...

"Shawn Milo" <ne********@linurati.net> wrote in message
news:c2**************************@posting.google.c om...
for(iCount = 0; iCount< drpBox.length; iCount ++){
if(drpBox [iCount] .value.match(regEx)){
drpBox.selectedIndex = iCount;
break;
}
for (iCount=0; iCount<drpBox.length; iCount++){
if (drpBox[iCount].value.match(regEx)){
drpBox.selectedIndex = iCount;
break;
}



不要使用break来终止循环;使测试条件失败 - 这是'b $ b为什么它在那里:


for(iCount = 0; iCount< drpBox.length& &

!drpBox [iCount] .value.match(regEx); iCount ++)

;

if(iCount!= drpBox.length )

drpBox.selectedIndex = iCount;


-

SC


Don''t use break to terminate a loop; make the test condition fail - that''s
why it''s there:

for (iCount=0; iCount<drpBox.length &&
!drpBox[iCount].value.match(regEx); iCount++)
;
if( iCount != drpBox.length )
drpBox.selectedIndex = iCount;

--
S.C.


Stephen Chalmers写道:
Stephen Chalmers wrote:
" Shawn Milo" <是ne ******** @ linurati.net>在消息中写道
新闻:c2 ************************** @ posting.google.c om ...
"Shawn Milo" <ne********@linurati.net> wrote in message
news:c2**************************@posting.google.c om...
for(iCount = 0; iCount< drpBox.length; iCount ++){
if(drpBox [iCount] .value.match(regEx)){
drpBox.selectedIndex = iCount;
break;
}
for (iCount=0; iCount<drpBox.length; iCount++){
if (drpBox[iCount].value.match(regEx)){
drpBox.selectedIndex = iCount;
break;
}



不要使用break来终止循环;使测试条件失败 - 那是为什么它在那里:

for(iCount = 0; iCount< drpBox.length&&
!drpBox [iCount] .value.match(regEx); iCount ++)
;
if(iCount!= drpBox.length)
drpBox.selectedIndex = iCount;

- -
SC



Don''t use break to terminate a loop; make the test condition fail - that''s
why it''s there:

for (iCount=0; iCount<drpBox.length &&
!drpBox[iCount].value.match(regEx); iCount++)
;
if( iCount != drpBox.length )
drpBox.selectedIndex = iCount;

--
S.C.




休息;完全可以接受它的使用方式。


您的代码也可以,但在我看来,并不是那么清楚。第一个

示例说,当匹配成功时,记录该值并离开

循环。同样,你的循环每次通过循环都会浪费任务,而且必须再对iCount进行一次测试< drpBox.length在退出之前。它还需要
需要在循环内对drpBox.length进行第二次(冗余)测试,如果它离循环定义更远,那么
可能导致奇怪的错误br />
您在没有意识到循环内的依赖性的情况下进行了更改。如果你想要

来避免休息;使用:


var selectedIndex = null;

for(iCount = 0; iCount< drpBox.length&& selectedIndex == null; iCount ++) {

if(drpBox [iCount] .value.match(regEx)){

selectedIndex = iCount;

}

}

if(selectedIndex!= null){

drpBox.selectedIndex = selectedIndex;

}

但是,我怀疑很多专业程序员会争论使用

break;在第一个例子中。只有那些认为破碎的人;并继续;应该使用

_never_会反对它。有时你想用break;或

继续;使代码更清晰易读:


for(var i = 0; i< list.length; i ++){

a = list [i ];

如果(a == null){

继续;

}

b = a.someProperty;

如果(!b){

继续;

}

c = b.anotherProperty;

如果(!c){

继续;

}

//等等

}


有了上面的循环,如果你嵌套了多个条件,你很快就会在第40列以外编写代码,几乎没有空间对于真正的代码
$ 80 $ b在第80列之前。将其作为手套的变体(< url:
http:/ /mindprod.com/jgloss/gauntlet.html />)将所有内容留在左边

边距附近,并在阅读代码时使排除条件非常清晰。


循环;测试;分配;打破;只是挑战的另一种变体。


-

| Grant Wagner< gw ***** @ agricoreunited.com>


*客户端Javascript和Netscape 4 DOM参考可从以下位置获得:

*
http:// devedge .netscape.com / library / ... ce / frames.html


* Internet Explorer DOM参考资料:

*
http://msdn.microsoft.com/研讨会/ a ... ence_entry.asp


* Netscape 6/7 DOM参考资料于:

* http://www.mozilla.org/docs/dom/domref/

*为Netscape 7 / Mozilla升级JavaScript的提示

* http://www.mozil la.org/docs/web-deve...upgrade_2.html



break; is perfectly acceptable the way it was used.

Your code works as well, but in my mind, isn''t nearly as clear. The first
example says when the match is successful, record the value and leave the
loop. As well, your loop does a wasted assignment every pass through the loop
and has to do one more test of iCount < drpBox.length before exiting. It also
requires a second (redundant) test of drpBox.length inside the loop, which
could lead to odd bugs if it were further away from the loop definition and
you made changes without realizing the dependancy within the loop. If you want
to avoid break; use:

var selectedIndex = null;
for (iCount = 0; iCount < drpBox.length && selectedIndex == null; iCount++) {
if (drpBox[iCount].value.match(regEx)) {
selectedIndex = iCount;
}
}
if (selectedIndex != null) {
drpBox.selectedIndex = selectedIndex;
}

However, I doubt many professional programmers would argue with the use of
break; in the first example. Only those who think break; and continue; should
_never_ be used would object to it. Sometimes you want to use break; or
continue; to make code clearer and easier to read:

for (var i = 0; i < list.length; i++) {
a = list[i];
if (a == null) {
continue;
}
b = a.someProperty;
if (!b) {
continue;
}
c = b.anotherProperty;
if (!c) {
continue;
}
// etc
}

With a loop like the one above, if you nested multiple conditions, you''d soon
be writing code beyond the 40th column, leaving almost no room for real code
before the 80th column. Doing it as a variation on the gauntlet (<url:
http://mindprod.com/jgloss/gauntlet.html />) leaves everything near the left
margin, and makes the exclusion conditions very clear as you read the code.

Loop; Test; Assignment; break; is just another variation on the gauntlet.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html


Stephen Chalmers写道:
Stephen Chalmers wrote:
" Shawn Milo" ; <是ne ******** @ linurati.net>在消息中写道
新闻:c2 ************************** @ posting.google.c om ...
"Shawn Milo" <ne********@linurati.net> wrote in message
news:c2**************************@posting.google.c om...




请不要写归属小说。



Please do not write attribution novels.

for(iCount = 0; iCount< drpBox.length ; iCount ++){
if(drpBox [iCount] .value.match(regEx)){
drpBox.selectedIndex = iCount;
break;
}
for (iCount=0; iCount<drpBox.length; iCount++){
if (drpBox[iCount].value.match(regEx)){
drpBox.selectedIndex = iCount;
break;
}



不要使用break来终止循环;让测试条件失败 -
这就是为什么它在那里:



Don''t use break to terminate a loop; make the test condition fail -
that''s why it''s there:




废话。这就是打破的意思。声明适用于:退出循环

而不知道测试条件和破坏开关()es如果下一个

案例条件不匹配或其默认语句应该

不能执行。我建议你阅读我们正在讨论的语言

约。


此外,你无视网络礼节并侵犯互联网/ Usenet

标准以及Usenet服务的可接受使用政策

提供商[1]为 me@here.com 你在来自:中使用标题不是

一个电子邮件地址。

PointedEars

___________

[1]< http: //www.tiscali.co.uk/aboutus/aup.html>



Nonsense. That''s what the "break" statement is good for: exiting loops
without knowing the test condition and breaking switch()es if the next
case-condition should not be matched or its default statements should
not be executed. I suggest you read on the language we are discussing
about.

Besides, you are disregarding Netiquette and violating Internet/Usenet
standards as well as the Acceptable Use Policy of your Usenet Service
Provider[1] as me@here.com which you use in your "From:" header is not
an e-mail address.
PointedEars
___________
[1] <http://www.tiscali.co.uk/aboutus/aup.html>


这篇关于使用JS搜索下拉框。 (代码示例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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