js对象转化成json数据格式
var UserObj =
{
userId:"",
userName:"",
roleId:"" ,
roleName:""
}
function submitData() {
var tb = document.getElementById(IDArray[0]);
if (tb)
{
var rows = tb.rows;
var ohjInfo;
for (var i = 1; i < rows.length; i++) {
var id = rows[i].cells[1].innerText;
var name = rows[i].cells[2].innerHTML;
var oDropDownList = rows[i].cells[3].childNodes[0];
var oText = oDropDownList.options[oDropDownList.selectedIndex].text;
var oValue = oDropDownList.options[oDropDownList.selectedIndex].value;
UserObj.userId = id;
UserObj.userName = name;
UserObj.roleId = oValue;
UserObj.roleName = oText;
var objStr = Serialize(UserObj); // js对象转化成json数据格式
ohjInfo += objStr;
}
return ohjInfo;
}
}
function Serialize(obj){
switch(obj.constructor){
case Object:
var str = "{";
for(var o in obj){
str += o + ":" + Serialize(obj[o]) +",";
} www.2cto.com
if(str.substr(str.length-1) == ",")
str = str.substr(0,str.length -1);
return str + "}";
break;
case Array:
var str = "[";
for(var o in obj){
str += Serialize(obj[o]) +",";
}
if(str.substr(str.length-1) == ",")
str = str.substr(0,str.length -1);
return str + "]";
break;
case Boolean:
return "\"" + obj.toString() + "\"";
break;
case Date:
return "\"" + obj.toString() + "\"";
break;
case Function:
break;
case Number:
return "\"" + obj.toString() + "\"";
break;
case String:
return "\"" + obj.toString() + "\"";
break;
}
}
aspx页面代码:
<input type="button" onclick='<%=string.IsNullOrEmpty(OnClientSubmit)?"retrun false;":string.Format("if({0}) {0}(submitData());return false;",OnClientSubmit)%>' alt="确定" />
aspx.cs后台代码:
public string OnClientSubmit
{
get
{
if (ViewState["OnClientSubmit"] != null)
{
return ViewState["OnClientSubmit"].ToString();
}
else
{
return "";
}
}
set { ViewState["OnClientSubmit"] = value; }
}
摘自 邹琼俊的专栏
相关新闻>>
- 发表评论
-
- 最新评论 更多>>