2011-12-20 67 views
1

我有這個基本的自動完成JavaScript,效果很好,但您需要對網頁進行硬編碼。我正在試圖做的是用Perl腳本將變量傳遞給自動完成JavaScript

工作的JavaScript代碼看起來像這樣的「自動完成」可變數據發送到頁面:

var CustomArray = new Array('an apple','alligator','elephant','pear','kingbird', 
          'kingbolt','kingcraft','kingcup','kingdom','kingfisher', 
          'kingpin','SML'); 

現在新的代碼是:

var CustomArray=new Array(Autocomplete); 

而且Perl腳本是將數據發送回瀏覽器看起來像這樣:

var Autocomplete = 'an apple','alligator','elephant','pear','kingbird', 
'kingbolt','kingcraft','kingcup','kingdom','kingfish er','kingpin','SML' 

我也試過

var Autocomplete = ['an apple','alligator','elephant','pear','kingbird', 
        'kingbolt','kingcraft','kingcup','kingdom','kingfisher', 
        'kingpin','SML'] 

但我得到:在自動完成'an apple','alligator','elephant','pear','kingbird','kingbolt','kingcraft','kingcup','kingdom','kingfish er','kingpin','SML'所有作爲一個字符串。

我似乎無法讓它正常工作。完整的HTML代碼如下。

<html> 
<head> 
<script language="javascript" type="text/javascript" src="http://www.comicinvasion.com/Code/Java/Autocomplete/Autocomplete.js"></script> 
<script language="javascript" type="text/javascript" src="http://www.comicinvasion.com/Code/Java/Autocomplete/Common.js"></script> 
<script language="JavaScript1.2" type="text/javascript" src="http://www.ComicInvasion.com/cgi-bin/Autocomplete.pl"></script> 


<script> 
var CustomArray=new Array(Autocomplete); 
</script> 

</head> 

<body> 

<input type='text' style='font-family:verdana;width:300px;font-size:12px' id='ACMP' value=''/> 

<script> 
var obj = actb(document.getElementById('ACOMP'),CustomArray); 
</script> 

</body> 
</html> 

回答

2

首先,它看起來像有一個錯字。您的輸入元素的idACMP,而您通過'ACOMP'getElementById

其次,您不提供Perl腳本的源代碼。這可能是這樣的:

#!/usr/bin/perl 

use utf8; 
use strict; use warnings; 
use CGI(); 

local $| = 1; 

print CGI::header(
    -type => 'text/javascript', 
    -charset => 'utf-8', 
); 

print <<JS; 
var Autocomplete = [ 
    'an apple','alligator','elephant','pear','kingbird', 
    'kingbolt','kingcraft','kingcup','kingdom','kingfisher', 
    'kingpin','SML' 
]; 
JS 

具有以下HTML,自動完成的作品:

<!DOCTYPE html> 
<html> 
<head> 

<script type="text/javascript" 
src="http://www.comicinvasion.com/Code/Java/Autocomplete/Autocomplete.js"></script> 

<script type="text/javascript" src="http://www.comicinvasion.com/Code/Java/Autocomplete/Common.js"></script> 

<!-- Replace with the URI of your script --> 
<script type="text/javascript" src="http://test:8080/cgi-bin/autocomplete.pl"></script> 

</head> 

<body> 

<input type='text' 
style='font-family:verdana;width:300px;font-size:12px' 
id='ACOMP' value=''> 

<script type="text/javascript"> 
var obj = actb(document.getElementById('ACOMP'), Autocomplete); 
</script> 

</body> 
</html> 

最後,我覺得很奇怪,你的JavaScript文件住在一個叫做Java目錄。

+0

看起來就像是在輸入錯誤。該ID感謝您的幫助! – Blnukem 2011-12-21 12:28:42

0

有Perl腳本返回此:

var CustomArray = "an apple, alligator".split(','); 

或者,如果它是這個沒關係太:

var CustomArray = "'an apple','alligator'".split(','); 

很顯然,我省略了項目的其餘部分那裏,但你會包括所有這些。

+0

試過了,我現在得到'',你可以在http://www.comicinvasion.com/autocomplete.htm看到,只需在字母「a」中輸入 – Blnukem 2011-12-20 23:58:40