bootStrap suggest js下拉框的使用,供大家参考,具体内容如下
HTML
<div> <p>选择商品:</p> <%@ include file="dataJs/item/itemSuggest.jsp" %> </div>
itemSuggest.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <link href="https://www.jb51.net/plugins/bootstrap/css/bootstrap.min.css"> <div> <div> <i></i> <input type="text"> <div> <ul role="menu"> </ul> </div> <!-- /btn-group --> </div> </div> <div></div>
js
(function() { /** * 请求json格式的数据 {value: [key:value,key2:value2]}; */ function init(url, dataList) { $.ajax({ type : 'post', async : false, url : url, success : function(data) { for (var i = 0; i < data.length; i++) { var obj = {}; obj.name = data[i].itemName; dataList.value.push(obj); } } }) return dataList; } /** * 插件绑定事件 */ function suggest(domID, dataList) { $("#" + domID).bsSuggest({ indexId : 1, // data.value 的第几个数据,作为input输入框的显示的内容 // indexKey: 2, //data.value 的第几个数据,作为input输入框的内容 data : dataList }).on('onDataRequestSuccess', function(e, result) { console.log('从 json参数中获取,不会触发 onDataRequestSuccess 事件', result); }).on('onSetSelectValue', function(e, keyword, data) { console.log('onSetSelectValue: ', keyword, data); }).on('onUnsetSelectValue', function() { console.log("onUnsetSelectValue"); }); } var dataList = { value : [] }; var url = hostUrl + "item/selectItemList.do"; init(url, dataList); console.info(dataList); suggest("suitName", dataList); }());
要引入的js
<script src="https://www.jb51.net/suggest/src/bootstrap-suggest.js"></script> <script src="https://www.jb51.net/dataJs/item/itemSuggest.js"></script>(即上方js代码) <script src="https://www.jb51.net/plugins/jQuery/jquery-2.2.3.min.js"></script>