有 <select id='comb'></select>
方法一:
前台:
function Initcombox() {
$.getJSON("InitComboxData.ashx?action=INITCOMBOBOX", function (data) {
$.each(data, function (i, item) {
$("<option></option>").val(item.Value).text(item.Text).appendTo($("#comb"));
//默认选择项
if (item.selected == true) {
$("#comb").val(item.Value);
}
});
});
return false;
}
后台:
public class InitComboxData : IHttpHandler
{
private const string ComboboxJsonData_Head = "[{";
private const string ComboboxJsonData_Item = "\"Value\":\"{0}\",\"Text\":\"{1}\"{2}";
private const string ComboboxJsonData_Selected = ",\"selected\":true";
private const string ComboboxJsonData_Splitter = "},{";
private const string ComboboxJsonData_Foot = "}]";
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
var action = context.Request.QueryString["action"];
if (!string.IsNullOrWhiteSpace(action))
{
switch (action.ToUpper())
{
#region 初始化下拉框
case "INITCOMBOBOX":
var rtn = new StringBuilder();
rtn.Append(ComboboxJsonData_Head);
context.Response.ContentType = "text/plain";
for (int i = 0; i > -12; i--)
{
if (i != 0)
{
rtn.Append(ComboboxJsonData_Splitter);
}
rtn.Append(string.Format(ComboboxJsonData_Item,
new[]
{
DateTime.Now.AddMonths(i).ToString("yyyyMM"),
DateTime.Now.AddMonths(i).ToString("yyyyMM"),
i == -2 ? ComboboxJsonData_Selected : string.Empty
}));
}
rtn.Append(ComboboxJsonData_Foot);
string s = rtn.ToString();
context.Response.Write(rtn.ToString());
break;
#endregion
}
}
else
{
context.Response.Write("");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
方法二:
前台:
<link href="Scripts/jquery_easyui_1.3/themes/default/easyui.css" type="text/css"
rel="stylesheet" />
<script src="Scripts/jquery_easyui_1.3/jquery-1.7.2.min.js" type="text/javascript"
language="javascript"></script>
<script src="Scripts/jquery_easyui_1.3/jquery.easyui.min.js" type="text/javascript"
language="javascript"></script>
<script src="Scripts/jquery_easyui_1.3/locale/easyui-lang-zh_CN.js" type="text/javascript"
language="javascript"></script>
function Initcombox() {
try {
var url = 'InitComboxData.ashx?action=INITCOMBOBOX';
$('#comb').combobox({
url: url,
valueField: 'Value',
textField: 'Text'
});
} catch (e) {
alert(e.message);
}
return false;
}
后台同上