是否可以将隐藏输入字段的值作为数组,然后将其传递给 Spring MVC Controller ?
function foo(){
var myArray = new Array();
myArray[0] = "Hello";
myArray[1] = "World!";
document.getElementById("hiddenInp").value=myArray;
}
然后在 Controller 中做类似的事情
@RequestMapping ...
public String test(HttpServletRequest request)
{
String[] myArray = request.getParameter("hiddenInp");
// Assuming that the name of the hidden input field is also hiddenInp
System.out.println(myArray[0] + myArray[1]);
...
}
如果我使用关联数组呢?其中索引是字符串而不是 int
最佳答案
您最好的选择是对数组进行字符串化,然后对其进行赋值。
element.value = JSON.stringify( ["Hello", "World"] );
结果值将是 JSON然后可以在服务器上解析的字符串,重新创建数组。如果您希望拥有类似于关联数组的东西,这种方法也适用于对象。
我应该指出,虽然 JSON
在当今的浏览器中得到了相当好的支持,但旧的浏览器可能不支持它。你可以polyfill the feature fairly easily在这些情况下:
关于Javascript 隐藏输入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10939840/