标签:
代码:
//页面参数传递类 var GoAndBackUrl = { createNewGo: function (strDesctinPage, strInfoDiv, strExtraParams) { var goUrl = {}; var desctinPage = strDesctinPage; var infoDiv = strInfoDiv; var extraParams = strExtraParams; goUrl.GoDectinUrl = function () { window.location.href = desctinPage + "?" + extraParams + "¶mers=" + this.MakeJson(); } goUrl.MakeJson = function () { var params = "{"; //获取控件集 var inputs = document.getElementById(infoDiv).getElementsByTagName("input"); var selects = document.getElementById(infoDiv).getElementsByTagName("select"); //制作json字符串 for (var i = 0; i < inputs.length; i++) { params += "\"" + inputs[i].id + "\"" + ":" + "\"" + $("#" + inputs[i].id).val() + "\","; } for (var j = 0; j < selects.length; j++) { params += "\"" + selects[j].id + "\"" + ":" + "\"" + selects[j].value + "\","; } params = params.substr(0, params.length - 1); //返回json return params + "}"; } return goUrl; }, createNewBack: function (strJson) { var backUrl = {}; var json = strJson; backUrl.DecrizeParams = function () { var params = eval("(" + json + ")"); for (var item in params) { //获取item var obj = document.getElementById(item); //input类型值填充 if (obj.nodeName.toLowerCase() == "input") { $("#" + item).val(params[item]); } //select类型值填充 if (obj.nodeName.toLowerCase() == "select") { var selectObj = document.getElementById(item); for (i = 0; i < selectObj.options.length; i++) { var curOptObj = selectObj.options[i]; var value = curOptObj.value; if (params[item] == value) { curOptObj.selected = true; } } } } } return backUrl; } };
发送
var goAction = GoAndBackUrl.createNewGo("目的页面url", "div的id", "额外的参数");//额外的参数需要自己按照格式拼写 goAction.MakeJson();//获取构造后的json字符串 goAction.GoDectinUrl();//直接前往目的页面
解析
var backMsg = GoAndBackUrl.createNewBack("json字段"); backMsg.DecrizeParams();
这里是后台写入隐藏字段,页面load的时候获取字段解析,填充。
这段代码只是div中的input和select两种控件,大多数情况下应该是够用了,事实上只需要修改一下json的格式也可以扩展的更加广泛精巧,比如{类型1:[id:value,id:value,id:value......],类型2:[id:value,id:value,id:value].......}这样看上去结构更好,也可以添加其他的空间类型
标签:
原文地址:http://my.oschina.net/hunjixin/blog/530139