JS如何传递一个对象

sunny007sun 2010-12-24 10:56:15


<table>
<tr><td>A <asp:DropDownList ID="MXBList" runat="server">
</asp:DropDownList></td>
<td>B<asp:DropDownList ID="GLList" runat="server">
</asp:DropDownList></td>
<td>C<asp:TextBox ID="QZtime" runat="server"></asp:TextBox></td>
<td>D<asp:TextBox ID="elesSm" runat="server"></asp:TextBox></td>
<td><input type="button" value="增加" onclick="ss()"/></td>



</tr>

</table>

上面的是一个用户空件中的HTML代码
下面是JS代码

function ss()
{
alert("111");
if(document.getElementById("MXBList").value=="无疾病")
{alert("请选择要添加的疾病!");}
else{
var arr = new Array();
arr[0]=document.getElementById("MXBList").value
arr[1]=document.getElementById("GLList").value
arr[2]=document.getElementById("QZtime").value
arr[3]=document.getElementById("elesSm").value
window.open("SQ_GR_MXBAJAX.aspx?arry="+arr);//这里传的值是一个字符串。

}

}





我想把 arr做为对象传到指定页面,应该如何处理。

...全文
1252 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
ycproc 2010-12-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 porschev 的回复:]

别放arr里就是了。。。

用个字符串拼接起来。。

用个特殊字符隔开

得到时split切开


或者AJAX提交一组JSON格式的数据
[/Quote]

正解 +1
chen_ya_ping 2010-12-24
  • 打赏
  • 举报
回复
给你一个例子:
SendForm.aspx:

<script src="../JS/jquery-1.4.2.js" type="text/javascript"></script>
<script src="../JS/json2.js" type="text/javascript"></script>
<script type="text/javascript">
var person = { "ID": 1001, "Name": "ping", "Age": 27 };
var jsonStr = JSON.stringify(person);
window.open("ReceiveForm.aspx?person="+jsonStr);
</script>

Recevie.aspx:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;

namespace JQuerWeb.JSON
{
public partial class ReceiveForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request["person"] != null)
{
JavaScriptSerializer jss=new JavaScriptSerializer();
Person p = jss.Deserialize<Person>(Request["person"].ToString());
Response.Write(string.Format("the person info, ID:{0} Name:{1} Age:{2}",p.ID.ToString(),p.Name,p.Age.ToString()));
}
}
}

public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
wnttmk 2010-12-24
  • 打赏
  • 举报
回复
可以试着用Json传递...
将他先用request的方式传递过去..
再在那边在服务端取出来..然后用一个变量取值
cs页面:
string _arr = request["arr"];
然后在aspx页面:
<script>
var arr = "<%=_arr%>";
//分裂或者是Json转换!
</script>
dreamloster 2010-12-24
  • 打赏
  • 举报
回复
session对象存放服务器端,本地JS不能对它赋值,用session不靠谱
cookie 浏览器可能禁用 不赞成

这里有必要使用数组,直接拼接字符串就可以吗

另:
if(document.getElementById("MXBList").value=="无疾病")
这句有问题
MXBList 是服务器控件在服务器端的标识,本地js不能通过这个ID获取
但可以通过ClientID属性获取
if(document.getElementById("<%=MXBList.ClientID %>").value=="无疾病")

wuyq11 2010-12-24
  • 打赏
  • 举报
回复
传递JSON字符串jsonstr="[{";
for(i=0;i<a.length;i++)
{
jsonstr += a[i] + ":" + "\"" + a[i] + "\",";
}
jsonstr = jsonstr.substring(0,jsonstr.lastIndexOf(','));
Issac25Name 2010-12-24
  • 打赏
  • 举报
回复
楼上有人说用字符串拼接,然后再分割,方法可以,但是如果属性字段多并且要传递的数据多的时候就不方便了,还有说用session或cookie,这个比较赞同!!!
chen_ya_ping 2010-12-24
  • 打赏
  • 举报
回复
考虑用Json数据。可以看看JSON2.JS这个
sunny007sun 2010-12-24
  • 打赏
  • 举报
回复
能给断代码吗?我刚学JS
flyerwing 2010-12-24
  • 打赏
  • 举报
回复
JSON.
porschev 2010-12-24
  • 打赏
  • 举报
回复
别放arr里就是了。。。

用个字符串拼接起来。。

用个特殊字符隔开

得到时split切开


或者AJAX提交一组JSON格式的数据
Ghost_Khz 2010-12-24
  • 打赏
  • 举报
回复
cookie.
yangxxxxxx66 2010-12-24
  • 打赏
  • 举报
回复
不能传对象,要么就用session
HolyPlace 2010-12-24
  • 打赏
  • 举报
回复
使用json
happy664618843 2010-12-24
  • 打赏
  • 举报
回复

用window.showModalDialog(url,param,);
接收:
var obj=window.dialogArguments;
alert(obj,name);
alert(obj.age);
  • 打赏
  • 举报
回复
[Quote=引用楼主 sunny007sun 的回复:]
HTML code

<table>
<tr><td>A <asp:DropDownList ID="MXBList" runat="server">
</asp:DropDownList></td>
<td>B<asp:DropDownList ID="GLList" runat="server">
</asp:DropDownList></td>
<td>C<asp:Te……
[/Quote]

做为什么“对象”呢? 老鼠的对象跟大象的对象,是完全不搭界的。
sunny007sun 2010-12-24
  • 打赏
  • 举报
回复
刚刚调试了下提示“Request["person"].ToString()”未将对象引用到实例。到我在URL上能看到传递的值!!!!!
chen_ya_ping 2010-12-24
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 sunny007sun 的回复:]

感觉好像是if (Request["person"] != null)
返回的是fales
[/Quote]
运行你的,要么调试,要么用个抓包的工具看看那个AJAX请求发出的URL中有没有person这个。
回头是岸 2010-12-24
  • 打赏
  • 举报
回复
在这种情况下 我觉得用 json 和字符拼接 都可以 但是不主张用session cookies ..........
sunny007sun 2010-12-24
  • 打赏
  • 举报
回复
没有异常啊~~~
var jsonStr = JSON.stringify(person);

alert(jsonStr);能看到是有值的
sunny007sun 2010-12-24
  • 打赏
  • 举报
回复
感觉好像是if (Request["person"] != null)
返回的是fales
加载更多回复(2)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧