Javascript 如何在 url 中传递字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3959609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass an array of string in a url
提问by Mercer
Hello i have an url in my jsp and i want to pass an array of string in this url to recover in my ActionForm
你好,我的 jsp 中有一个 url,我想在这个 url 中传递一个字符串数组以在我的 ActionForm 中恢复
回答by Andrey
If you are dealing with something simple like a list of numeric ids, i would just run through the check boxes, create a comma separated list, and assign it to a query string parameter. On the other side i would split the string. If the values are more complex you have to consider escape characters. Also if you are dealing with a long list, the url is not the best way to pass this data.
如果您正在处理诸如数字 id 列表之类的简单内容,我只需遍历复选框,创建一个逗号分隔列表,并将其分配给查询字符串参数。另一方面,我会拆分字符串。如果值更复杂,则必须考虑转义字符。此外,如果您正在处理一个长列表,则 url 不是传递此数据的最佳方式。
回答by Nikita Rybak
You can use 'standard' html way of passing arrays of data: http://mywebsite/mypage?myarray=value1&myarray=value2&myarray=value3
. Then you can fetch all values of parameter myarray
from request object (if framework doesn't provide more elegant ways of handling arrays).
您可以使用的数组数据传递的“标准” HTML方式:http://mywebsite/mypage?myarray=value1&myarray=value2&myarray=value3
。然后你可以myarray
从请求对象中获取参数的所有值(如果框架没有提供更优雅的处理数组的方法)。
But seeing your comment, I would recommend to leave JavaScript and just declare a form for it.
If you need a link (not button), you can always submit form from it. Something like <a href="javascript:$('#myForm').submit();">...</a>
但是看到您的评论,我建议您离开 JavaScript 并为其声明一个表单。
如果您需要链接(不是按钮),您可以随时从中提交表单。就像是<a href="javascript:$('#myForm').submit();">...</a>
回答by zod
Try Json encode
尝试 Json 编码
http://code.google.com/p/json-simple/
http://code.google.com/p/json-simple/
Check this
检查这个