支持搜索过滤和ajax的select下拉框
XML/HTML Code
- <div class="content">
- <div class="row">
- <div class="cell">
- <div class="desc">这个例子使用静态的选项数据</div>
- <select id="select1" style="width: 100%;">
- <option value="-1">湖北</option>
- <option value="1">湖南</option>
- <option value="2">北京</option>
- <option value="3">广东</option>
- <option value="4">广西</option>
- <option value="5">上海</option>
- <option value="6">浙江</option>
- <option value="7">江苏</option>
- </select>
- </div>
- <div class="cell">
- <div class="desc">这个例子不提供搜索过滤功能</div>
- <select id="select2" style="width: 100%;">
- <option value="-1">湖北</option>
- <option value="1">湖南</option>
- <option value="2" selected>北京</option>
- <option value="3">广东</option>
- <option value="4">广西</option>
- <option value="5">上海</option>
- <option value="6">浙江</option>
- <option value="7">江苏</option>
- </select>
- </div>
- </div>
- <div class="row">
- <div class="cell">
- <div class="desc">这个例子从file.json加载数据。不带初始选项。带搜索过滤功能。</div>
- <select id="select3" style="width: 100%;">
- <option value="-1">---</option>
- </select>
- </div>
- <div class="cell">
- <div class="desc">这个例子从file.json加载数据。带初始选项。不带搜索过滤功能。</div>
- <select id="select5" style="width: 340px;">
- <option value="1">abc</option>
- </select>
- </div>
- </div>
- <div class="row">
- <div class="cell">
- <div class="desc">Note: 插件在json数据格式不正确时会得到错误的结果。</div>
- <select id="select4">
- <option value="-1">---</option>
- </select>
- </div>
- </div>
- </div>
JavaScript Code
- <script>
- /* This parser won't respect "---" selection */
- function dataParserA(data, selected) {
- retval = [ { val: "-1" , text: "---" } ];
- data.forEach(function(v){
- if(selected == "-1" && v.val == 3)
- v.selected = true;
- retval.push(v);
- });
- return retval;
- }
- /* This parser let's the component to handle selection */
- function dataParserB(data, selected) {
- retval = [ { val: "-1" , text: "---" } ];
- data.forEach(function(v){ retval.push(v); });
- return retval;
- }
- /* Create select elements */
- $("#select1").tinyselect();
- $("#select2").tinyselect({ showSearch: false });
- $("#select3").tinyselect({ dataUrl: "file.json" , dataParser: dataParserA });
- $("#select4").tinyselect({ dataUrl: "failure.json" });
- $("#select5").tinyselect({ dataUrl: "file.json" , dataParser: dataParserB });
- $("#select2").on("change",function() {
- console.log($(this).val());
- });
- $("#havoc").show()
- </script>
原文地址:http://www.freejs.net/article_biaodan_892.html