当前页面: 开发资料首页 → JSP 专题 → select 数组转化成字符串
select 数组转化成字符串
摘要: select 数组转化成字符串
大意如题,字符之间用逗号隔开
比如有:
北京
上海
杭州
传给下张页面时 显示为:
北京,上海,杭州
具体要怎么转换,给个具体的代码,谢谢
没看明白……
java代码可以简单这么写:
public class Test {
public static String convert(String[] a) {
if (a == null || a.length==0) return null;
StringBuffer s = new StringBuffer();
for (int i =0; i < a.length; i++) {
s.append(a[i]);
if (i != a.length - 1) {
s.append(",");
}
}
return s.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
String[] a = {"北京", "上海", "杭州"};
System.out.println(convert(a));
}
}
要是js的话方法差不多
谁能给我一个js做的,谢谢
取得select数组的长度,循环取得每一子项值连接字符串就可以了啊,很简单的。
在js里面还是
var test1=new Array(3);
test1[0]='北京'
test1[1]='上海'
test1[2]='杭州'
var test2=test1[0];
for(var i=1;i
{
test2=','+test1[i];
}
alert(test2);
不好意思,有点小错误:
var test1=new Array(3);
test1[0]='北京'
test1[1]='上海'
test1[2]='杭州'
var test2=test1[0];
for(var i=1;i
{
test2=test2+','+test1[i];
}
alert(test2);
String[] city = {"北京","上海","杭州"};
System.out.println(StringUtils.join(city,","));
当值为动态是时候呢?
当值为动态。。。每条数据填个checkbox..
提交时候
用javascript检查是否选中
选中则加上。。。建议试用字符串
没必要用字符数组