简体   繁体   English

在Input中使用外部Jquery

[英]Use external Jquery in Input

I want to use the typeahead code(external file, country.js) 我想使用typeahead代码(外部文件,country.js)

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

But this code above, it is a separate file, not in the same jsp, it is in the country.js file. 但是上面这个代码,它是一个单独的文件,不在同一个jsp中,它在country.js文件中。 When I use the same code, however in jsp I do this: 当我使用相同的代码时,不过在jsp中我这样做:

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

What about my input, 我的意见怎么样

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

In other words, I use the same code in country.js does not work, and when I put the same code in jsp, just adding works. 换句话说,我在country.js中使用相同的代码不起作用,当我在jsp中放入相同的代码时,只需添加作品。

I wanted typeahead to work without being in jsp, but in country.js 我希望typeahead能够在没有jsp的情况下工作,但是在country.js中

I use to import .js: 我用来导入.js:

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

Any tips, help? 任何提示,有帮助吗?

enter image description here 在此输入图像描述

Thank you! 谢谢!

In your country.js file, wait for the page to be ready first: country.js文件中,等待页面首先准备好:

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

Any other tips? 还有其他提示吗? The reference to the other file, country.js, is being successfully made. 对其他文件country.js的引用正在成功完成。 But the code does not work. 但代码不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM