1

我想使用的預輸入代碼(外部文件,country.js)在使用輸入外部的Jquery

var $input = $("#input-search_address"); 
$input.typeahead({source:['United States', 'United Kingdom', 'Germany','China']}); 

但高於此代碼,它是一個單獨的文件,而不是在同一個JSP,它是在country.js文件。當我使用相同的代碼,但在JSP中我這樣做:

<script> 
    var $input = $("#input-country"); 
    $input.typeahead({source:['United States', 'United Kingdom', 'Germany','China']}); 
    </script> 

那我的輸入,

<input type="text" id="input-country" class="input" placeholder="Country"> 

換句話說,我使用相同的代碼在country.js不起作用,當我把相同的代碼放入jsp中時,只需添加工作即可。

我想提前鍵入沒有JSP是工作,但在country.js

我用導入的.js:

<script src="javascript/country.js" type="text"></script> 

任何提示,幫助嗎?

enter image description here

謝謝!

+0

您是否在等待DOM準備好? – mehulmpt

回答

0

在你country.js文件,等待頁面要準備好第一:

$(function() { 
    // now write your code 
}); 
+0

是的,我使用; var $ input = $(「#input-search_address」); $ input.typeahead({source:['United States','United Kingdom','Germany','China']}); – Gean

0

任何其他提示嗎?對另一個文件country.js的引用正在成功完成。但是代碼不起作用。