javascript - 在输入中使用外部 Jquery

标签 javascript jquery twitter-bootstrap jsp twitter-bootstrap-3

我想使用输入代码(外部文件,country.js)

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中使用相同的代码时,只需添加就可以了。

我希望 typeahead 能够在不使用 jsp 的情况下工作,而是在 Country.js 中工作

我用来导入.js:

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

有什么建议、帮助吗?

enter image description here

谢谢!

最佳答案

在您的 country.js 文件中,首先等待页面准备就绪:

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

关于javascript - 在输入中使用外部 Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42738910/

相关文章:

javascript - 使用 Node 使用用户 ID 在 github 上创建要点

在启动时设置值时,jQuery 验证器 "required"不起作用

javascript - 我可以使用 jQuery 找到绑定(bind)在元素上的事件吗?

javascript - jquery/ajax - 在任何表单上禁用提交按钮点击 3 秒

html - 图像在浏览器预览中质量下降

javascript - 具有不同主题标签 URL ID 的 Bootstrap 轮播

javascript - 如何使用ajax从行中发布数据

javascript - 如何在 Bootstrap 中垂直对齐输入元素?

javascript - 窗口 onload 事件有时会丢失

css - 衡量单个 CSS 规则对页面渲染速度影响的工具?