Besten,
PS: Ik ben nog niet zo goed in javascript. Dit is een bestaande code die ik heb herschreven.
Ik probeer een option-list te filteren door enkel naar de eerste letters te kijken.
index.html
filterlist.js
Hij geeft steeds een error op de pagina zodra er iets in de selectie wordt getypt.
Volgems mij zit de fout in functie Left.
Ofwel geeft hij niet de juiste parameters mee of 'this.selectobj.text' moet een andere waarde bevatten.
PS: Ik ben nog niet zo goed in javascript. Dit is een bestaande code die ik heb herschreven.
Ik probeer een option-list te filteren door enkel naar de eerste letters te kijken.
index.html
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
| <html>
<head>
<SCRIPT src="filterlist.js" type=text/javascript></SCRIPT>
</head>
<body>
<FORM name=myform action="">
<SELECT size=10 name=myselect>
<OPTION>Keanu Reeves
<OPTION>Laurence Fishburne
<OPTION>Monica Bellucci
<OPTION>Daniel Bernhardt
<OPTION>Nona Gaye
<OPTION>Lachy Hulme
<OPTION>Nathaniel Lees
<OPTION>Harry J. Lennix
<OPTION>Matt McColm
<OPTION>Carrie-Anne Moss
<OPTION>Collin Chou
<OPTION>Genevieve O'Reilly
<OPTION>Harold Perrineau Jr.
<OPTION>Jada Pinkett Smith
<OPTION>Adrian Rayment
<OPTION>Neil Rayment
<OPTION>Bruce Spence
<OPTION>Hugo Weaving
<OPTION>Lambert Wilson
<OPTION>Anthony Wong
<OPTION>Walter O'Riley
<OPTION>Zach Taylor
<OPTION>Yuri Andropov
<OPTION>Xavier
<OPTION>Tom Selleck
<OPTION>Heather Graham
<OPTION>Ursula Andrews
<OPTION>Victoria Jackson
<OPTION>Vivian Vance
<OPTION>Star Jones
<OPTION>Steve McQueen</OPTION>
</SELECT>
<SCRIPT type=text/javascript>
var myfilter = new filterlist(document.myform.myselect);
</SCRIPT>
<INPUT onkeyup=myfilter.set(this.value) name=regexp>
<INPUT onclick="myfilter.reset();this.form.regexp.value=''" type=button value=Clear>
</FORM>
</body>
</html> |
filterlist.js
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
| function filterlist(selectobj)
{
// HTML SELECT object
this.selectobj = selectobj;
// Make a copy of the options array
this.optionscopy = new Array();
for (var i=0; i < selectobj.options.length; i++)
{
this.optionscopy[i] = new Option();
this.optionscopy[i].text = selectobj.options[i].text;
this.optionscopy[i].value = selectobj.options[i].value;
}
// This method resets the select list to the original state.
// It also unselects all of the options.
this.reset = function()
{
this.set("");
}
//--------------------------------------------------
this.set = function(pattern)
{
// This method removes all of the options from the select list,
// then adds only the options that match the pattern regexp.
// It also unselects all of the options.
// In case of a regexp error, returns false
var loop=0, index=0, regexp, e;
// Clear the select list so nothing is displayed
this.selectobj.options.length = 0;
// Loop through the entire select list
for (loop=0; loop < this.optionscopy.length; loop++)
{
// Check if we have a match
if (Left(this.optionscopy[loop].text, this.selectobj.text))
{
// We have a match, so add this option to the select list
this.selectobj.options.length = index + 1;
this.selectobj.options[index].text = this.optionscopy[loop].text;
this.selectobj.options[index].value = this.optionscopy[loop].value;
this.selectobj.options[index].selected = false;
// Increment the index
index++;
}
}
function Left(option, str)
{
var n = String(str).length;
if (String(option).substring(0,n) = String(str).substring(0,n));
{
return true;
}
else
{
return false;
}
}
}
} |
Hij geeft steeds een error op de pagina zodra er iets in de selectie wordt getypt.
Volgems mij zit de fout in functie Left.
Ofwel geeft hij niet de juiste parameters mee of 'this.selectobj.text' moet een andere waarde bevatten.