1. 程式人生 > >easyui-combobox 下拉列表的JSON獲取資料例子

easyui-combobox 下拉列表的JSON獲取資料例子

1. 使用easyui需要匯入的包:

<script type="text/javascript" src="js/easyui/jquery.min.js"></script>

<link rel="stylesheet" type="text/css"
    href="js/easyui/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="js/easyui/themes/icon.css" />

<script type="text/javascript" src="js/easyui/jquery.easyui.min.js"></script>

1.index.jsp頁面寫一個簡單的下拉類表

<script type="text/javascript">
    $(function() {
        $("#city").combobox({
            valueField : "id",
            textField : "text",
            url : "city.jsp",
        });
    $("#city").combobox("select",function(value){
             return value+1;
            });
    });
</script>

<span>城市:<input name="city"  id="city" class="easyui-combobox" /> </span>

1.city.jsp

<%@page import="net.sf.json.JSONArray"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (int i = 1; i < 6; i++) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("id", i + "");
            map.put("text", "city" + i);
            list.add(map);
        }

   //轉化為JSON資料
        String str = JSONArray.fromObject(list).toString();
        response.getWriter().write(str);
        //System.out.println(str);
    
    %>