实现目的:通常在项目中,编辑页面在前后台需要一个一个框赋值,取值操作,小伙伴们普遍都会感觉繁琐,麻烦.;
实现思路:利用json对象化键值的思想;
好处:方便快速开发,提高开发效率,减少重复性代码;
缺点:还不完善,只支持文本框,复选框,下拉框几种控件;
根本原因:纯粹是为了学习与交流;
示例:只要传一个对象即可获得和给下面的控件赋值;
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATIAAAD9CAIAAACwbWxHAAAOnUlEQVR4nO3d3W7b5h2A8fcudBk80EX4KniiK/CJkFsIfUAjyHkuYEE6YSmxda5X1N26YW3QjyldVCe1Izsu7ciwl9Ax4ATgDmjRFL9Eii/Fv6jnByGQ+fFKEfTkJSVHUn/48gcuXLiIuigfgDBkCYhDloA4ZAmIQ5aAOGQJiEOWgDhkCYjT2iwPDsc7u3uPnzzlsuqXnd29g8Nx00+opWptluOjs9PJZdP3AhqcTi73D06avhdLVWOWSpUbvOz2+cZHZxpHQ7PIcnb1PHN3L3FXyBIZyHJ29Wwq+T/O3T1/y7LN5yPLNiHL2dXls1xgds0aZ859z0WWbUKWs6sXyrLgaNGFZefhuciyTchydnX5c8viWcZGCK9rOcnMyHLUVR1n6JYayrF6SnVH1e8TFkWWs6vrny1ja3W98KN1tnR7xgZZNogsZ1cvMcsiGxRHlm1ClrOrl/KSj584oK0umeVoYCulooejjtULbrdj9MLjWnfodOILb7MMto9ujOUgy9nVdZ5bFrmJ1I0L/L2yZsu7ec8dOhumHSy1zY3pCefdyadj9ezBKLqXY1nMmY0gy9nVWg9iY5uFjWXtUmuWvu/7/qh7ey9uU4y2GttrMLC7KauwDGQ5u7q2LIvsUuWwtshs2Zke0IazZXaWna5p22Z3On9iqchydrWwLDXOlo7V61lOsHT2IDY8+RxZtxuEe402OLFsAllG1qVVUWqDoneimYPY8AhWGYYRPY7t3C7uhgEHr/QMb1eVfucTFZFlZF12dVnnhAtnWfwlpYKyfp2A6W4VkeV0Rfn3GBeuqP5zy/hLO1gtZNkS/E5sm5Bl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fVsT70/8pLTcgS62OVsuS/QWNNrEyWnnd9dHoh8+J5100/PGiVlckSWB9kCYhDloA4ZAmIQ5aAOGQJiEOWgDhkCYhDloA4ZAmIQ5aAOGQJiEOWgDhkCYhDloA4ZAmIQ5aAOGQJiEOWgDhkCYhDloA4QrN8+PDhvXv3Nht17969hw8fNv1IYB1JzNLa2nrw4IHrup8a5brugwcPrK2tph8PrB2JWfb7/bOzs5ubm3eNurm5OTs76/f7TT8eWDsSs9zc3Pz06VOzTQY+ffq0ubnZ9OOBtSM0y48fP/5PgI8fP5Illk9ulpfFKKW0bxkiSzRCbpYXxSilgj+jcrYshSzRCKFZ3tzcnBeglIpdSV7Plz/+zc0NWWL55GY5KUApFbsSu56/cC6yRCPkZvl2nmC6i14PJbcsslkSWaIRQrP88OHDq1xKqfDP2MKsjYtsGfPhwweyxPKtapaxOJOi2xSslywhhNws9wtQSuX8GC4J/0zKH58s0Qi5Wf5agFIqej2Qv1nyxxxkiUYIzfLq6upFAUqp6JXon6mbpf6Y4+rqiiyxfHKz/KUApVT4Z+qVIj/mIEs0QmiWnucNC1BK5fyYujw40C0y+HA49DyPLLF8crP8WQCyRCOEZvn+/fufBHj//j1ZYvkkZtnv98fj8WQy+aFRk8lkPB7z36CxfBKz3Nra2t7ePj4+ft+o4+Pj7e3tLT40BEsnMUvf9+/fv9/v9+v/GK08/X7//v37TT8SWEdCswTWGVkC4pAlIA5ZAuKQJSAOWQLikCUgDlkC4pAlIA5ZAuKQJSAOWQLikCUgDlkC4pAlIA5ZAuKQJSAOWQLikCUgDlkC4pAlIA5ZAuKQJSAOWQLikCUgDlkC4pAlIA5ZAuKQJSAOWQLikCUgDlkC4pAlIA5ZAuL8H1NDPVHm2gvsAAAAAElFTkSuQmCC" alt="" />
主要扩展代码:
(function ($) {
$.fn.setValues = function (options) {
var obj = JSON.parse(options);
$("input[type='text'][datakey]").each(function () {
$this = $(this);
$this.val(obj[$this.attr("datakey")]);
});
$("input[type='checkbox'][datakey]").each(function () {
$this = $(this);
$this.attr("checked", obj[$this.attr("datakey")]);
});
$("select[datakey]").each(function () {
$this = $(this);
$nationtype = obj[$this.attr("datakey")];
var str = "";
$this.find("option[value='" + $nationtype + "']").attr("selected", true);
});
}
$.fn.getValues = function () {
var objValues = new Object();
$("input[type='text'][datakey]").each(function () {
$this = $(this);
objValues[$this.attr("datakey")] = $this.val();
});
$("input[type='checkbox'][datakey]").each(function () {
$this = $(this);
objValues[$this.attr("datakey")] = $this.attr("checked") == "checked" ? true : false;
});
return JSON.stringify(objValues);
}
})(jQuery);
测试页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="FormEditor.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FormEditor.Demo</title>
<script src="Scripts/ajaxhelper.js"></script>
<script src="Scripts/jquery-1.9.1.js"></script>
<script src="Scripts/formEditor.js"></script>
<script>
$(document).ready(function () {
ajaxmethod('Handler/UsersHandler.ashx?actiontype=getnations', function (data) {
var $obj = JSON.parse(data);
$("select[datakey]").each(function () {
$this = $(this);
var str = "";
for (var i = ; i < $obj.length; i++) {
$objson = $obj[i];
str += "<option value='" + $objson["Code"] + "'>" + $objson["Name"] + "</option>";
}
$this.append(str);
});
ajaxmethod("Handler/UsersHandler.ashx?actiontype=getusers", function (data) {
//通过插件方法赋值-参数为json对象
$(this).setValues(data);
});
});
//通过插件方法取值-获得的结果为json对象
//getValues();
});
function getValues() {
return alert($(this).getValues());
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="formeditor">
用户名:<input id="Text1" type="text" datakey="Name" /><br />
<br />
密码:<input id="Text2" type="text" datakey="Pass" /><br />
<br />
邮箱:<input id="Text3" type="text" datakey="Email" /><br />
<br />
启用:<input id="Text4" type="checkbox" datakey="IsUse" /><br />
民族:<select id="sel1" datakey="NationType"></select>
<%-- 性别:<input id="Radio1" type="radio" datakey="rdoMale" />男
<input id="Radio2" type="radio" datakey="rdoFeMale" />女<br />--%>
<br />
<br />
<input id="btnSave" type="button" value="保存" /><br />
</div>
</form>
</body>
</html>
Hanlder.ashx 文件代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json; namespace FormEditor.Handler
{
/// <summary>
/// UsersHandler 的摘要说明
/// </summary>
public class UsersHandler : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string actiontype = context.Request.QueryString["actiontype"];
switch (actiontype)
{
case "getusers":
context.Response.Write(JsonConvert.SerializeObject(GetUser()));
break;
case "getnations":
context.Response.Write(JsonConvert.SerializeObject(GetNations()));
break;
default:
break;
}
}
private List<Nationality> GetNations()
{
List<Nationality> Nation = new List<Nationality>();
Nationality n1 = new Nationality() { Code = , Name = "汉族" };
Nationality n2 = new Nationality() { Code = , Name = "回族" }; Nation.Add(n1);
Nation.Add(n2); return Nation;
}
private Users GetUser()
{
Users users = new Users()
{
ID = ,
Email = "jackandmary@qq.com",
Name = "jack",
Pass = "",
Sex = ,
IsUse = false,
NationType =
};
return users;
} public bool IsReusable
{
get
{
return false;
}
}
}
}
实体类代码:
public class Users
{
public Users() { }
public int ID { get; set; }
public string Name { get; set; }
public string Pass { get; set; }
public int Sex { get; set; }
public string Email { get; set; }
public bool IsUse { get; set; }
public int NationType { get; set; }
} public class Nationality
{
public int Code { get; set; }
public string Name { get; set; }
}
说明:暂时就多说了。
PS:楼主技术积累比较浅,但是一直保持着浓厚的兴趣,现在纠结的是对jquery的源码基本不懂,想要研究却不知道从哪里下手,再摸索。
这个功能很简单,但是楼主还是从0点开始到现在花了4个多小时,完成了一部分,以后再往更深层次优化吧,希望可以为一些新手朋友带来一点帮助!