使用升级版的 Bootstrap typeahead v1.2.2
上次介绍了 Bootstrap 2 中附带的 typeahead,功能强大,但是使用起来不太方便,作者 Terry Rosen 已经升级了一个新版本 v1.2.2,作出了很大的改进。 下载地址 https://github.com/tcrosen/twitter-bootstrap-typeahead 使用环境 Twitter Bootstrap 2.0 jQue
上次介绍了 Bootstrap 2 中附带的 typeahead,功能强大,但是使用起来不太方便,作者 Terry Rosen 已经升级了一个新版本 v1.2.2,作出了很大的改进。
下载地址
https://github.com/tcrosen/twitter-bootstrap-typeahead
使用环境
- Twitter Bootstrap 2.0+
- jQuery 1.7+
页面准备
<span><span>link </span><span>href</span><span>="/path/to/bootstrap.css"</span><span> rel</span><span>="stylesheet"</span><span>></span> <span><span>script </span><span>src</span><span>="/path/to/jquery.js"</span><span> type</span><span>="text/javascript"</span><span>></span><span>script</span><span>></span> <span><span>script </span><span>src</span><span>="/path/to/bootstrap-typeahead.js"</span><span> type</span><span>="text/javascript"</span><span>></span><span>script</span><span>></span></span></span></span>
脚本
$(myElement).typeahead(options);
事件
事件 | 说明 |
---|---|
grepper | Filters relevant results from the source. |
highlighter | Highlights any matching results in the list. |
itemSelected | 当选中一个项目时的回调函数.
|
lookup | Determines if source is remote or local and initializes the search. |
matcher | Looks for a match between the query and a source item. |
render | Renders the list of results. |
select | Selects an item from the results list. |
sorter | 排序结果. |
初始化参数
基本使用
如果使用本地数据的话直接使用 source
<span>var</span> mySource = [{ id: 1, name: 'Terry'}, { id: 2, name: 'Mark'}, { id: 3, name: 'Jacob'<span>}]; $(</span>'#myElement'<span>).typeahead({ source: mySource });</span>
如果使用 Ajax 的话,可以直接指定 url,注意,现在的版本要求必须使用对象形式的数据源,默认显示文本为对象的 name 属性,可以通过初始化参数的 display 配置,默认的标识属性为 id ,可以使用 val 进行配置。
$('#myElement'<span>).typeahead({ ajax: </span>'/path/to/mySource'<span> });</span>
使用 Ajax
$(<span>function</span><span> () { $(</span>'#product_search'<span>).typeahead({ ajax: { url: </span>'@Url.Action("AjaxService")'<span>, timeout: </span>300, <span>//</span><span> 延时</span> method: 'post'<span>, triggerLength: </span>3, <span>//</span><span> 输入几个字符之后,开始请求</span> loadingClass: <span>null</span>, <span>//</span> 加载数据时, 元素使用的样式类<span> preDispatch: </span><span>null</span><span>, // 发出请求之前,调用的预处理方法 preProcess: </span><span>null // Ajax 请求完成之后,调用的后处理方法</span><span> }, display: </span>"name", <span>//</span><span> 默认的对象属性名称为 name 属性</span> val: "id", <span>//</span><span> 默认的标识属性名称为 id 属性</span> items: 8, <span>//</span><span> 最多显示项目数量</span> itemSelected: <span>function</span> (item, val, text) { <span>//</span><span> 当选中一个项目的时候,回调函数</span> <span> console.info(item); } }); });</span>
如果我们需要在请求中,除了递进搜索的参数之外,添加额外的请求参数怎么办呢,可以通过 preDispatch 进行额外处理,需要注意的是,一定要返回一个对象,这个对象将用来使用 jQuery 的 Ajax 方法作为参数。
$(<span>function</span><span> () { $(</span>'#product_search'<span>).typeahead({ ajax: { url: </span>'@Url.Action("AjaxService")'<span>, timeout: </span>300, <span>//</span><span> 延时</span> method: 'post'<span>, triggerLength: </span>3, <span>//</span><span> 输入几个字符之后,开始请求</span> loadingClass: <span>null</span>, <span>// </span> preDispatch: <span>function</span><span> (query) { </span><span>var</span> para = { other: 'xxxxxxxxx'<span> }; para.query </span>=<span> query; </span><span>return</span><span> para; }, preProcess: </span><span>function</span><span> (result) { </span><span>return</span><span> result; } }, display: </span>"name", <span>//</span><span> 默认的对象属性名称为 name 属性</span> val: "id", <span>//</span><span> 默认的标识属性名称为 id 属性</span> items: 8, <span>//</span><span> 最多显示项目数量</span> itemSelected: <span>function</span> (item, val, text) { <span>//</span><span> 当选中一个项目的时候,回调函数</span> <span> console.info(item); </span><span>//</span><span> console.info($("#product_search").val());</span> <span> } }); });</span>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to use Bootstrap to get the value of the search bar: Determines the ID or name of the search bar. Use JavaScript to get DOM elements. Gets the value of the element. Perform the required actions.

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

Use Bootstrap to implement vertical centering: flexbox method: Use the d-flex, justify-content-center, and align-items-center classes to place elements in the flexbox container. align-items-center class method: For browsers that do not support flexbox, use the align-items-center class, provided that the parent element has a defined height.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text
