下拉框搜尋 select2 的使用[轉載]
阿新 • • 發佈:2018-12-11
原文地址:
https://m.imooc.com/article/14472
使用場景是:一個下拉框有太多值 需要搜尋做匹配
- 載入檔案
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
- html程式碼
<select name="hospital_id" id="searchHospital">
<option value="0">請選擇醫院</option>
<foreach name="hospitalData" item="list">
<option value="{$list.hospital_id}"
<if condition="$_GET['hospital_id'] eq $list['hospital_id']">selected</if>>
{$list .hospital_name}
</option>
</foreach>
</select>
- js程式碼
<script>
$(function(){
$('#searchHospital').select2({
placeholder: "Select a state"
});
});
</script>