首页 新闻 会员 周边

如何将select的option全部回传服务器

0
悬赏园豆:100 [已解决问题] 解决于 2008-11-25 12:13

由于需求需要

需要做2个mutiply形的select

一个表示可选分类listbox1

一个标识已选分类listbox2

可以通过button实现添加和删除(我是通过一下方法实现)

 

   function  AddOption()
{
document.getElementById(
"ListBox2").appendChild(document.getElementById("ListBox1").options[document.getElementById("ListBox1").selectedIndex]);
document.getElementById(
"ListBox1").removeChild(document.getElementById("ListBox1").options[document.getElementById("ListBox1").selectedIndex]);

}
function DeleteOption()
{
document.getElementById(
"ListBox1").appendChild(document.getElementById("ListBox2").options[document.getElementById("ListBox2").selectedIndex]);
document.getElementById(
"ListBox2").removeChild(document.getElementById("ListBox2").options[document.getElementById("ListBox2").selectedIndex]);
}

 

同时需要将 已选分类listbox2 里的所有值回传到服务器(主要是要知道如何实现这个,下面的为我自己实现时候出现的问题,能够回答最好,不能回答也无所谓)

在做回传的时候我碰到了一个问题

就是如果通过javascript添加select的option后

 for (i=0 ; i<document.getElementById("ListBox2").length;i++)

这样遍历就会出异常

而为什么出我不打清楚

问题补充: 原则上是不希望做成回传服务器添加的 这样会改逻辑 最好就用javascript解决 最好别用jQuery 目前对jQuery还不是很了解 可能会存在看不懂的问题 回传是提交的时候发出的 和选择一个分类和删除一个分类不在一起进行
小眼睛老鼠的主页 小眼睛老鼠 | 老鸟四级 | 园豆:2731
提问于:2008-11-25 09:05
< >
分享
最佳答案
0

在提交之前执行一下for循环,先把ListBox2的所有项的selected设置为true,然后再执行submit事件,这样就可以把所有ListBox2的项都提交到服务器端了

丁学 | 专家六级 |园豆:18730 | 2008-11-25 10:48
其他回答(4)
0

学习。。

Jared.Nie | 园豆:1940 (小虾三级) | 2008-11-25 09:19
0

for (i=0 ; i<document.getElementById("ListBox2").length;i++)

你是想遍历select还是options?

如果想遍历options,应该是

for (i=0 ; i<document.getElementById("ListBox2").options.length;i++)

你遍历这些options的时候,把他们的值拼凑起来,比如用逗号隔开,然后放在一个hidden里面

lola | 园豆:255 (菜鸟二级) | 2008-11-25 09:37
0

没学JS学习下

子夜星辰 | 园豆:1613 (小虾三级) | 2008-11-25 10:03
0

通过javascript在服务器端来改变某些服务器端控件(例如listbox)的值,在服务器端listbox的值会被Viewstate还原,推荐使用拼凑字符串的方式

吴畏 | 园豆:426 (菜鸟二级) | 2008-11-25 11:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册